Difference between DbSet.Add (entity) and entity.State = EntityState.Added

Possible duplicate:
What is the difference between IDbSet.Add and DbEntityEntry.State = EntityState.Added?

What is the difference between DbSet.Add (entity) vs entity.State = EntityState.Added ? I have several examples using both to add an object to a DbContext, but am not sure which one is preferred.

I saw some testing of the Condition Separately "and decided what to use the repository in their implementation.

public void Add(T entity) { var entry = DbContext.Entry(entity); if (entry.State == EntityState.Detached) { DbSet.Add(entity); } else { entry.State = EntityState.Added; } } 

Any idea? Thanks!

+3
entity-framework
source share
1 answer

There is no difference between these parameters, since under the hood they both call the same method (for example, AddObject in ObjectContext).

+2
source share

All Articles