Failed to update foreign key in Entity Framework 6

I am trying to do a simple foreign key update, but the script will never be sent.

Here is the code I'm using:

using (var db = new MyContext()) { db.Entry<Contact>(newContact).State = EntityState.Modified; newContact.ContactOwner = db.Person.Find(3); db.SaveChanges(); } 

EF6 updates the rest of the column in the Persons table, but does not update the Contact_Id in Persons table.

Personality:

 public class Person { public int Id { get; set; } public string Name { get; set; } public List<Contact> ContactList { get; set; } } 

The contact person:

 public class Contact { public int Id { get; set; } public string Email { get; set; } public string TelNo { get; set; } public Person ContactOwner { get; set; } } 

What am I missing here?

Please, help!

+6
source share
3 answers

Because you are working with an independent association. You can either

  • Adding and removing relationships from ContactList , but you need to get from Person .

     db.Entry(newContact).State = EntityState.Modified; var p1 = db.Set<Person>().Include(p => p.ContactList) .FirstOrDefault(p =>p.Id == 1); p1.ContactList.Remove(newContact); var p3 = db.Set<Person>().Include(p => p.ContactList) .FirstOrDefault(p => p.Id == 3); p3.ContactList.Add(newContact); db.SaveChanges(); 
  • Or you can use a disabled object, but you need to manually manage the relationship.

     db.Entry(newContact).State = EntityState.Modified; var p1 = new Person { Id = 1 }; db.Entry(p1).State = EntityState.Unchanged; var p3 = new Person { Id = 3 }; db.Entry(p3).State = EntityState.Unchanged; var manager = ((IObjectContextAdapter)db).ObjectContext.ObjectStateManager; manager.ChangeRelationshipState(newContact, p1, item => item.ContactOwner, EntityState.Deleted); manager.ChangeRelationshipState(newContact, p3, item => item.ContactOwner, EntityState.Added); db.SaveChanges(); 

PS

You may need to reconsider adding the foreign key value, make it easier, update the foreign key by simply specifying Id.

See this post for more details.

+8
source

This is what I finished building until late in the morning, chould / should be reorganized yet ...

  protected static async Task<int> SaveEntity<t>(t obj) where t : BaseModel { try { using (DatabaseContext db = GetDbContext()) { //get the basemodel/fk reference properties IEnumerable<PropertyInfo> props = obj.GetType().GetProperties().Where(p => p.PropertyType.BaseType == typeof(BaseModel)); if (obj.Id <= 0) {//insert db.Entry(obj).State = EntityState.Added; //set fk reference props to unchanged state foreach (PropertyInfo prop in props) { Object val = prop.GetValue(obj); if (val != null) { db.Entry(val).State = EntityState.Unchanged; } } //do insert return await db.SaveChangesAsync(); } else {//update //get the posted fk values, and set them to null on the obj (to avaid dbContext conflicts) Dictionary<string, int?> updateFKValues = new Dictionary<string, int?>(); foreach (PropertyInfo prop in props) { BaseModel val = (BaseModel)prop.GetValue(obj); if (val == null) { updateFKValues.Add(prop.Name, null); } else { updateFKValues.Add(prop.Name, val.Id); } prop.SetValue(obj, null); } //dbContext creation may need to move to here as per below working example t dbObj = (t)db.Set(typeof(t)).Find(new object[] { obj.Id }); //this also differs from example //update the simple values db.Entry(dbObj).CurrentValues.SetValues(obj); //update complex values foreach (PropertyInfo prop in props) { Object propValue = null; if (updateFKValues[prop.Name].HasValue) { propValue = (BaseModel)db.Set(prop.PropertyType).Find(new object[] { updateFKValues[prop.Name] }); } prop.SetValue(dbObj, propValue); if (propValue != null) { db.Entry(propValue).State = EntityState.Unchanged; } } //do update return await db.SaveChangesAsync(); } } } catch (Exception ex) { ExceptionHelper.Log(ex); throw; } } 
+1
source

This mainly happens because EntryState.Modified just looks for scalar properties (primitive types) and with an independent association (your case) you don't have it.

There are several ways to achieve this, @Yuliam pointed out some of them and here you can find more.

+1
source

Source: https://habr.com/ru/post/974135/


All Articles