DbSet.Add & DbSet.Remove Versus using EntityState.Added & EntityState.Deleted

asp.net-mvc c# entity-framework entity-framework-6

Question

In entity framework 6 there are multiple way of adding / removing entities, for example for adding entities, we can use:-

b.Students.Add(student);
db.SaveChanges();

OR

db.Entry(student).State = EntityState.Added;
db.SaveChanges();

and similarly when Deleting object:-

db.Entry(studentToDelete).State = EntityState.Deleted;
db.SaveChanges();

OR

db.Remove(StudentToDelete)
db.SaveChanges();

I did not find much resources that talks about the differences , and most of the online tutorials mention both approaches as if they are the same. but i have read a reply on an article link is that using dbset.Add set the status for the entity and all its related entities/collections to added, while using EntityState.Added will adds also all the related entities/collections to the context but leaves them as unmodified, and same thing applies for dbset.Remove & EntityState.Deleted

so are these differences correct ?

Edit

As i understand that graph operation means that both the parent and child got deleted/added , if the parent is marked for deletion or for addition. so i did these two tests :-

using (var db = new TestContext())
{
    var a = new Department { DepartmentName = "Shipping" };
    var b = new Employee { FirstName = "Bob", LastName = "Dodds", Department = a };
    db.Entry(b).State = EntityState.Added;
    db.SaveChanges();
}



using (var db = new TestContext())
{
    var a2 = new Department { DepartmentName = "Production" };
    var b2 = new Employee { FirstName = "Sarah", LastName = "Gomez", Department = a2 };
    db.Employees.Add(b2);
    db.SaveChanges();
}

where both have added the dept & the employee. so can you adivce on this please?

1
4
5/23/2017 12:01:00 PM

Popular Answer

From Lerman & Miller's DbContext book (p. 80):

Calling DbSet.Add and setting the State to Added both achieve exactly the same thing.

Which is:

If the entity is not tracked by the context, it will start being tracked by the context in the Added state. Both DbSet.Add and setting the State to Added are graph operations— meaning that any other entities that are not being tracked by the context and are reachable from the root entity will also be marked as Added. If the entity (the root entity - my addiditon) is already tracked by the context, it will be moved to the Added state.

Note that this is incorrectly answered in the question you refer to. The book was written with EF 4.3 in mind, and it doesn't mention any breaking change since EF 4.1 when DbContext was introduced.

Setting an entity's state to deleted (either through DbSet.Remove() or by setting Entry(entity).State to Deleted) is not a graph operation. It only affects the entity's state, not the entities in its object graph.

The latter is true for any entity state except Added.

An exception to this rule is when cascaded delete is configured, both in the database and in the EF model. In a many-to-many association with a hidden junction table, cascaded delete is the default, so junction records are always deleted when a root entity is marked for delete (either by setting its state to Deleted or by removing it from its DbSet).

For instance, let A and B have a many-to-many association. The junction table AB is only in the database, not in the class model. If you fetch an A from the database (without Include()-ing its Bs) and delete it, the A is deleted and al of its AB records, but no Bs are deleted.

When in a one-to-many association cascaded delete is configured the 'many' entities will be deleted when a '1' entity is deleted. If A and B have a one-to-many association, deleting an A will also delete its Bs.

1
7/25/2015 10:05:22 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow