我有一个实体集,当我将它打印到控制台时,它看起来像这样:
EntitySet {entityContext: resource_dbEntities, _expression: undefined, elementType: function, person: function, createNew:function…}
_defaultType: function person(){
_expression: EntitySetExpression
collectionName: "person"
createNew: function person(){
elementType: function person(){
entityContext: resource_dbEntities
eventHandlers: undefined
name: "person"
person: function person(){
roles: undefined
stateManager: EntityStateManager
tableName: "person"
tableOptions: undefined
__proto__: EntitySet有没有办法找出entityset中有多少元素?我不确定它是否被填充,但不确定如何检查。
比如"myEntitySet.size"?
发布于 2013-06-14 05:48:32
如果您无论如何都要对整个数据库进行查询,就像您在评论中提到的那样,您可以使用查询DbSet的计数,即使这将从数据库中获取所有行。
static void Main(string[] args)
{
using (var context = new EntityContext())
{
context.Entities.Add(new MyEntity());
context.Entities.Add(new MyEntity());
var myEntityCount = context.Entities.Count();
Console.WriteLine("The context now tracks {0} entities", context.Entities.Local.Count);
context.SaveChanges();
}
using (var context = new EntityContext())
{
Console.WriteLine("The db had {0} entities when I queried it", context.Entities.Count());
context.SaveChanges();
}
}
class MyEntity
{
public int MyEntityId { get; private set; }
}
class EntityContext:DbContext
{
public DbSet<MyEntity> Entities { get; set; }
}在数据库中有大量数据的情况下,您不会希望这样做,并且您将不得不求助于编写sql来只返回表的计数,而不是获取所有内容。
https://stackoverflow.com/questions/17095010
复制相似问题