Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 实体框架是否会自动加载我的所有数据(不是引用,惰性加载已关闭)?_Visual Studio_Entity Framework - Fatal编程技术网

Visual studio 实体框架是否会自动加载我的所有数据(不是引用,惰性加载已关闭)?

Visual studio 实体框架是否会自动加载我的所有数据(不是引用,惰性加载已关闭)?,visual-studio,entity-framework,Visual Studio,Entity Framework,我有一个数据库第一实体框架项目。对于我添加的每个实体,都会将一个集合添加到该实体的DbContext中。我在DbContext构造函数中显式地设置了LazyLoadingEnabled=false。如果我打断下面的代码并检查CustomerDepartments的计数,我将得到表的总计数。如果我只是添加一个新记录,我希望在添加之前计数为0,之后计数为1。我在无状态环境中使用它,因此加载整个表只是为了添加一条记录似乎很荒谬。我做错了什么 using (Model.SupportEntities s

我有一个数据库第一实体框架项目。对于我添加的每个实体,都会将一个集合添加到该实体的DbContext中。我在DbContext构造函数中显式地设置了
LazyLoadingEnabled=false
。如果我打断下面的代码并检查CustomerDepartments的计数,我将得到表的总计数。如果我只是添加一个新记录,我希望在添加之前计数为0,之后计数为1。我在无状态环境中使用它,因此加载整个表只是为了添加一条记录似乎很荒谬。我做错了什么

using (Model.SupportEntities support = new Model.SupportEntities(_state.Credentials, _handler.ReadWriteConnectionString)) 
{
    Model.CustomerDepartment department = Json.JsonConvert.DeserializeObject<Model.CustomerDepartment>(_insertObject);
    support.CustomerDepartments.Add(department);
    support.SaveChanges();
    _state.ReturnNewIdAsJson(department.CustomerDepartmentID);
}
使用(Model.SupportEntities support=new Model.SupportEntities(_state.Credentials,_handler.ReadWriteConnectionString))
{
Model.CustomerDepartment department=Json.JsonConvert.DeserializeObject(_insertObject);
support.CustomerDepartments.Add(部门);
support.SaveChanges();
_state.ReturnNewIdAsJson(department.CustomerDepartmentID);
}

您似乎误解了
DbContext
DbSet
的工作原理

如果您有一个记录EntityFramework SQL调用的工具,那么最好尝试
Clutch.Diagnostics.EntityFramework

DbSet
上调用
IEnumerable.Count()
时,Entity Framework运行以下查询

SELECT COUNT(*) FROM TableName;
但它不会加载整个表

您想要的行为的实际调用是

support.CustomerDepartments.Local.Count;

support.ChangeTracker.Entries().Count()
这些不会影响数据库

您必须记住,
DbSet
是数据库表的抽象,因此对其调用Count()应该会告诉您表中有多少行


顺便说一句,仅供参考。惯例是将您的
DbContext
命名为
SupportContext
<代码>模型命名类或名称空间表明它们是您的POCO。

也许我误解了您的问题,但您不应该将LazyLoadingEnabled设置为true吗?
support.ChangeTracker.Entries<T>().Count()