Reputation: 1
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?
Upvotes: 5
Views: 2544
Reputation: 109080
From Lerman & Miller's DbContext book (p. 80):
Calling
DbSet.Add
and setting theState
toAdded
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. BothDbSet.Add
and setting theState
toAdded
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 asAdded
. If the entity (the root entity - my addiditon) is already tracked by the context, it will be moved to theAdded
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 B
s) and delete it, the A
is deleted and al of its AB
records, but no B
s 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 B
s.
Upvotes: 2