locked
Confusing articles and documentation about the differences (if any) between System.Data.EntityState.Add & DbSet.Add RRS feed

Answers

  • User-821857111 posted

    The article you referenced from Magnus Montin is incorrect, as you have already confirmed with your own test.  Quote from Programming Entity Framework: DbContext 

    Calling DbSet.Add and setting the State to Added both achieve exactly the same thing.
    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 is already tracked
    by the context, it will be moved to the Added state. So far we’ve only added entities that
    aren’t tracked by the context, but a little later in this chapter you’ll see that being able
    to set the state of an entity that is already tracked to Added is important. 
      
    Whether you choose DbSet.Add or setting the State property is simply a matter of which
    is more convenient in the code you are writing.

    [Chapter 4: Working with Disconnected Entities Including N-Tier Applications p 80]

    The authors of this excellent book are Julia Lerman, an MVP who specialises in EF, and Rowan Miller, who is the Program Manager for RF at Microsoft. so if anyone knows, Rowan does.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 15, 2016 9:07 PM