使用的DbContext集<而不是暴露上下文(); T&GT上下文、而不是、DbContext、LT

2023-09-03 03:25:25 作者:☆ぺ忄存暖亽┈

有没有执行下列操作时,任何差别:

Are there any differences when doing the following:

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

与使用设置&LT; T&GT; 上下文的方式:

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?

推荐答案

用户属性是为了方便加入,所以你不必记住所有的你的表和相应的类是什么,你可以使用智能感知看到所有表的情况下被设计为与交互。最终的结果是功能上等同于使用设置&LT; T&GT;

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