Entity Framework does not update Foreign Key object

后端 未结 3 1157
南笙
南笙 2020-12-21 00:46

I\'m new to Entity Framework and this behavior confuses me:

    [Table(\"ClinicProfile\")]
    public class ClinicProfile
    {
        [Key]
        [Databa         


        
相关标签:
3条回答
  • 2020-12-21 01:27

    You just need to set ContactDataId, when updating

    clinicProfile.ContactData = contactData;
    clinicProfile.ContactDataId = contactData.Id;
    SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified;
    SharedContext.Current.SaveChanges();
    
    0 讨论(0)
  • 2020-12-21 01:29

    you just misplaced the foreign key property

     [Table("ClinicProfile")]
        public class ClinicProfile
        {
            [Key]
            [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
            public int Id { get; set; }
    
            //[ForeignKey("ContactData")] here the wrong place
            public int ContactDataId { get; set; }
            [ForeignKey("ContactDataId")] // here the correct place
            public ContactData ContactData { get; set; }
        }
    
        [Table("ContactData")]
        public class ContactData
        {
             [Key]
             [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
             public int Id { get; set; }
    
             ...
        }
    

    and when you want to set the foreign key data, you can either set the ContactDataId value or retrieve the ContactData from Database as object and set it in the ClinicProfile

    // according to your data posted later
    
    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Modify(ClinicProfile clinicProfile, ContactData contactData, AdressData adressData)
            {
                ViewBag.Id = clinicProfile.Id;
    
                if (ModelState.IsValid)
                {
                    if (clinicProfile.Id != 0)
                    {
                        // here you want to tell the SharedContext to attach the contactData to the clinicProfile 
                        // you need to retrieve the lastVersion of contactData from db
                        var currentContactData=SharedContext.Current.ContactData.Single(t=>t.Id=contactData.Id);
                        // update the changed data in the currentContactData
                        clinicProfile.ContactData =currentContactData;  // instead of contactData;
                        clinicProfile.AdressData = adressData;
                        clinicProfile.AdressDataComposed = adressData.ComposeData();
                        clinicProfile.ContactDataComposed = contactData.ComposeData();
    
                        SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified;
                        SharedContext.Current.SaveChanges();
    
                        Config.SaveClinicPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
                        Config.SaveClinicPreviewPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
    
                        return View(new ClinicProfileComposite { AdressData = adressData, ClinicProfile = clinicProfile, ContactData = contactData });
                    }
    
                    {
                        clinicProfile.ContactData = contactData;
                        clinicProfile.AdressData = adressData;
                        clinicProfile.AdressDataComposed = adressData.ComposeData();
                        clinicProfile.ContactDataComposed = contactData.ComposeData();
    
                        SharedContext.Current.Entry(clinicProfile).State = EntityState.Added;
                        SharedContext.Current.SaveChanges();
    
                        Config.SaveClinicPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
                        Config.SaveClinicPreviewPhoto(clinicProfile.ClinicImageUpload, clinicProfile.Id);
    
                        return RedirectToAction("Info", new { id = clinicProfile.Id });
                    }
                }
    
                ViewBag.Id = clinicProfile.Id;
                return View(new ClinicProfileComposite { AdressData = adressData, ClinicProfile = clinicProfile, ContactData = contactData });
            }
    

    // as result of comments discussion

    if you want to update contactData , you need to tell the context that contactData was modified by setting its state to modified and as you mentioned in your last post, it will work if you make the following:

    SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified; SharedContext.Current.Entry(contactData).State = EntityState.Modified; SharedContext.Current.SaveChanges();

    hope that this will help you

    0 讨论(0)
  • 2020-12-21 01:33

    if you set to existing ClinicProfile new ContactData you should first save new ContactData to db, second get its new Id and third update ClinicProfile with new ContactDataId.

    clinicProfile.ContactDataId = newContactData.Id;
    SharedContext.Current.Entry(clinicProfile).State = EntityState.Modified;
    SharedContext.Current.SaveChanges();
    

    if you want to update existing ContactData properties then you should save only it.

    SharedContext.Current.Entry(existingContactData).State = EntityState.Modified;
    SharedContext.Current.SaveChanges();
    
    0 讨论(0)
提交回复
热议问题