I do not know the author of this blog. I know the authors of the book DbContext though (although not personally). They know EF inside out. Therefore, when on page 80 they write
Calling DbSet.Add and setting State to Added both achieve exactly the same.
I know what I'm doing. They do the same thing:
If an object is not tracked by context, it will begin to be tracked by the context in the Added state. Both DbSet.Add and the State setting in Added are DbSet.Add operations - which means that any other objects that are not tracked by the context and inaccessible from the root entity will also be marked as Added .
I also know from experience that it works that way. But to eliminate any doubts in the EF source code, both DbSet.Add and DbEntityEntry.State (when installing Added ) arrive at the same point in the ObjectContext , which does the actual work:
public virtual void AddObject(string entitySetName, object entity)
This is a feature that continues to mislead developers who start working with EF, as can be seen from the large number of questions in StackOverflow that ask something like "how are my objects duplicated?". Julia Lerman wrote the entire blog , explaining why this could happen.
This ongoing misconception made the EF team decide to change this behavior in EF7.
Perhaps the blog writer you are linking to was one of those misguided developers.
Gert arnold
source share