System.Data.Entity.DbSet vs System.Data.Entity.Infrastructure.DbQuery

时间:2014-09-28 04:26:56

标签: entity-framework

有人可以解释一下实体框架中两者之间的区别。

例1:

obj = new TicketsEntities();
var depts = obj.DEPARTMENTs.Select( x => x);
string str = depts.GetType().ToString();

在这种情况下str print --- System.Data.Entity.Infrastructure.DbQuery`1 [LINQu.Models.DEPARTMENT]

例2:

obj = new TicketsEntities();
var depts = obj.DEPARTMENTs;
string str = depts.GetType().ToString();

在这种情况下str print --- System.Data.Entity.DbSet`1 [LINQu.Models.DEPARTMENT]

在任何一种情况下,当我们遍历depts时,我们得到相同的结果,那么两者之间有什么区别,哪一个更受欢迎?

1 个答案:

答案 0 :(得分:1)

The DbSet represents the set of data and can manipulate the data by exposing methods like Add, Update, Remove. The DbQuery represents a Linq query that is executed on a set of data. It does not have the Add, Update and Remove methods.

In your case I think there is no real difference, but for simplicity sake I would pick your second example since the Select(x=>x) is not neccessary.