Using DbContext Set() instead of exposing on the context

.net c# dbcontext ef-code-first entity-framework

Question

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?

1
29
12/4/2012 8:57:14 PM

Accepted Answer

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>.

20
12/4/2012 8:16:44 PM

Popular Answer

You get a benefit with the former method when using Code-First migrations, as new entities will be detected as such automatically. Otherwise, I'm quite certain they are equivalent.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow