7

I am trying to do a simple update to the foreign key but the script never get sent over.

Here is the code I am using:

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

EF6 update the rest of the column in the Persons table but it is not updating the Contact_Id in Persons table.

Person entity:

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

Contact entity:

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!

foo0110
  • 165
  • 2
  • 3
  • 11
  • Could you please show the `Persons` class ? – Yuliam Chandra Aug 21 '14 at 03:40
  • @YuliamChandra I have added the Person and Contact class – foo0110 Aug 21 '14 at 03:47
  • Is it typo `newPerson.Contact` doesn't match with `public Person ContactOwner { get; set; }`? and Does `newPerson` a new entity or a modified entity ? – Yuliam Chandra Aug 21 '14 at 03:50
  • @YuliamChandra Edited the question, it should be the other way around. The "newContact" is a modified entity. – foo0110 Aug 21 '14 at 03:55
  • Still typo `newContact.Person` and `public Person ContactOwner { get; set; }`, Does `newContact` has existing reference to any `Person` ? – Yuliam Chandra Aug 21 '14 at 04:10
  • @YuliamChandra It should be correct now, sorry I overlooked it. the "newContact" has existing reference to another "Person" entity with ID=1 – foo0110 Aug 21 '14 at 04:12
  • In my case tracking was disabled on EF Core. So it was not able to track associated entity changes. Fixed this by using other efcore functions with tracking. – Abhay Shiro Feb 20 '20 at 06:59

4 Answers4

8

Since you are working with independent association. You can either

  • Adding and removing the relationship from ContactList, but you need to retrieve from both 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 disconnected 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 might need to reconsider adding foreign key value, to make everything easier, updating foreign key just by mentioning the Id.

See this post for more information.

Community
  • 1
  • 1
Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • I tried the code you provided but still not seeing the Person_Id in Contact table get generated in the update script by EF6. – foo0110 Aug 21 '14 at 05:20
  • Just notice your edited answer, after going through the link provided and your answer, I added the "PersonId" property into Contact entity, and everything works just fine now! I am using both "Independent association" & "Foreign key association" together. – foo0110 Aug 21 '14 at 06:36
2

This is what I ended up building till the late hours of the morning, could/should be refactored some more...

   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;
        }
    }
SharpC
  • 6,974
  • 4
  • 45
  • 40
DPrins
  • 21
  • 1
1

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

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

Community
  • 1
  • 1
fabriciorissetto
  • 9,475
  • 5
  • 65
  • 73
0

This solution worked for me when needing to update the main object and it's foreign key objects attached.

    public virtual async Task<Result<TEntity>> Update<TEntity>(TEntity entity) where TEntity : class
    {
        Result<TEntity> returnResult = new Result<TEntity>();

        try
        {
            //get the fk reference properties
            IEnumerable<PropertyInfo> props = entity.GetType().GetProperties().Where(p => p.PropertyType.BaseType == typeof(object));

            //set fk reference props to modified state
            foreach (PropertyInfo prop in props)
            {
                Object val = prop.GetValue(entity);
                if (val != null)
                {
                    dbContext.Entry(val).State = EntityState.Modified;
                }
            }


            dbContext.Entry(entity).State = EntityState.Modified;
            await dbContext.SaveChangesAsync();
            returnResult.SetSuccess(result: entity);
        }
        catch (Exception e)
        {
            log.Error(e);
            returnResult.SetError(exception: e);
        }
        
        return returnResult;
    }
Jeff Penner
  • 361
  • 3
  • 3