Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does DbSet Add return an entity instance instead of void?

The DbSet<TEntity>.Add method returns an entity. I would normally have expected an Add operation to have a void return type.

When I look at the EntityFramework source code, I see the following implementation:

    public virtual TEntity Add(TEntity entity)
    {
        Check.NotNull(entity, "entity");

        GetInternalSetWithCheck("Add").Add(entity);
        return entity;
    }

GetInternalSetWithCheck returns an InternalSet<TEntity>

The Add method of InternalSet<TEntity> interestingly enough has a void return type in its signature:

public virtual void Add(object entity)

My concern is whether or not I need to be careful with when I do modifications to an entity with relation to when it is added to a DbSet.

E.g. are there cases where

var entity = new MyEntity();
_dbSet.Add(entity);
entity.SomeDatModifyingMethod();
_dbContext.SaveChanges();

might give different behavior than

var entity = new MyEntity();
entity.SomeDatModifyingMethod();
_dbSet.Add(entity);
_dbContext.SaveChanges();

or different behavior than:

var entity = new MyEntity();
entity = _dbSet.Add(entity);
entity.SomeDatModifyingMethod();
_dbContext.SaveChanges();

In the basic default implementation, it wouldn't ever matter, because it just always returns exactly the same instance. However, the Add method is virtual, so it can be overridden (though in the public source code, the only override is a test double - but I'm not sure that source code actually includes e.g. the SqlServer support implementation).

Why does DbSet Add return an entity instance instead of void?

like image 852
Nathan Avatar asked Jan 26 '14 03:01

Nathan


People also ask

What is the use of DbSet in Entity Framework?

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext. Set method.

How does DbContext change state of entity?

This can be achieved in several ways: setting the EntityState for the entity explicitly; using the DbContext. Update method (which is new in EF Core); using the DbContext. Attach method and then "walking the object graph" to set the state of individual properties within the graph explicitly.

What is entity type in C#?

Entity types can be mapped to database views using the Fluent API. EF will assume that the referenced view already exists in the database, it will not create it automatically in a migration. C# Copy. modelBuilder.

What is meant by DbContext and DbSet?

DbContext generally represents a database connection and a set of tables. DbSet is used to represent a table. Your code sample doesn't fit the expected pattern.


1 Answers

It allows you to write a "Find or Add" pattern

var person = context.People.Find(ssn) ?? context.People.Add(new Person
{
   SocialSecurityNumber = ssn,
   FirstName = "John",
   LastName = "Doe"
});
like image 140
Dennis Flagg Avatar answered Sep 19 '22 19:09

Dennis Flagg