Is it possible to update the Collection
of an entity with relationship one-to-many
through its navigation property
?
public class Foo
{
public virtual IList<Bar> Bars {get; set;}
public int FooID { get; set; }
}
public void UpdateFoo(Foo foo)
{
dbContext.Set<Foo>.Attach(foo);
dbContext.Entry(foo).State = EntityState.Modified;
}
The behavior of EF
is to add the foo.Bars
objects to the table, without removing the records that do not exist in the collection. The workaround I found was to dbSet.RemoveRange
of objects not in foo.Bars
to delete them from the database.
What is the most appropriate way to update a collection?
EDIT -----------------
Looking at this Daniel post Simmons , it seems that the 6.x
of entity framework
version does not support this feature. The behavior done by ORM
is to modify the value of the foreign keys to null
, not deleting the registry.
Is there a recommended approach to perform this operation?