0

I have update a generic update method as follows

public virtual void Update<TEntity>(TEntity entity, string modifiedBy = 
null)           where TEntity : class,IEntity
{
  using (var context = new BanyanDbContext())
  {                
    entity.ModifiedDate = DateTime.UtcNow;
    entity.ModifiedBy = modifiedBy;
    var existingEntiy = context.Set<TEntity>().Find(entity.Id);
    context.Entry(existingEntiy).CurrentValues.SetValues(entity);
    context.Entry(existingEntiy).State = EntityState.Modified;                                
    Save(context);
  }
}

This method is working fine for updating non reference type values, but CurrentValues.SetValues() does not set or update navigation properties. How can I set navigation property in this scenario.

  • Have you seen https://stackoverflow.com/questions/11705569/using-the-entrytentity-currentvalues-setvalues-is-not-updating-collections? – Miamy Dec 07 '18 at 08:18

1 Answers1

0
public TEntity RemoveNavigationProperties(TEntity input)
        {

            foreach (var item in input.GetType().GetProperties().Where(x => x.PropertyType.Namespace == input.GetType().Namespace))
            {
                item.SetValue(input, null);
            }

            return input;
        }

This worked for me. (I just needed to null them)

Morten Bork
  • 1,413
  • 11
  • 23