Entity Framework does not update Foreign Key object

后端 未结 3 1171
南笙
南笙 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: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();
    

提交回复
热议问题