Using DbContext Set<T>() instead of exposing on the context

Dismissile picture Dismissile · Dec 4, 2012 · Viewed 34.3k times · Source

Are there any differences when doing the following:

public class UsersContext : DbContext
{
    public DbSet<User> Users { get; set; }
}

versus using the Set<T> method of the context:

public class UsersContext : DbContext
{
}

var db = new UsersContext();
var users = db.Set<User>();

These effectively do the same thing, giving me a set of Users, but are there any big differences other than you are not exposing the set through a property?

Answer

Servy picture Servy · Dec 4, 2012

The Users property is added for convenience, so you don't need to remember what all of your tables are and what the corresponding class is for it, you can use Intellisense to see all of the tables the context was designed to interact with. The end result is functionally equivalent to using Set<T>.