- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有以下代码从客户表中检索数据
var customers= context.CustomerEntities.Include("Addresses").Select(Mapper.Map).ToList();
映射器函数,将实体对象映射到业务对象,看起来像这样
internal static Customer Map(CustomerEntity entity)
{
if (entity == null)
return null;
return new Customer
{
Id = entity.Id,
Name = entity.Name,
Addresses = Map(entity.Addresses)
};
}
现在,上面的代码运行良好。
但是,当我尝试这样做时:
var customers= context.CustomerEntities.Select(Mapper.Map).ToList();
我收到错误消息:在执行 Mapper 函数时,已经有一个打开的 DataReader 与此命令关联,必须先将其关闭
。
现在我知道要解决这个问题,我必须在我的连接字符串中设置 multipleactiveresultsets=True
。我试过了,确实解决了我的问题。
但是,当我运行 SQL 分析器时,从 Entity Framework 查询所有客户也会自动检索所有地址,即使我不需要它们。
除了必须设置 multipleactiveresultsets=True
之外,还有解决方法吗?我不希望地址一直延迟加载。
最佳答案
我相信这是因为对于每个 Customer
,select 语句都会导致再次读取数据库。为什么不先执行 ToList()
然后应用映射(Select
),例如:
var customers= context.CustomerEntities.ToList().Select(Mapper.Map);
我相信这会先带来数据,然后再进行映射,您不会遇到这个问题。
关于c# - entityframework 已经有一个与此命令关联的打开的 DataReader,必须先将其关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16065684/
全部: 我们使用 ASP.NET Web 窗体开始了我们的项目。 我们的 ASP.NET C# web 应用程序在以下环境中使用 .NET Framework 4 ASP.NET Web Forms.
我在尝试为 Entity Framework Core 添加迁移到 Code First 项目时遇到错误,这里是详细信息... 我创建了一个新的 ASP.Net Core Web 项目(VS 2017
Error 1 Copying file bin\EntityFramework.SqlServer.xml to obj\Debug\Package\PackageTmp\bin\Entit
我有一个WPF应用程序,我使用了MVVM体系结构,数据库sql server compact 4。 首先是Entityframework.sqlserverCompact数据库,我使用了通用存储库模式
例如,将 products.Where(p => p.LastModifiedOn > someDate) 总是以相同的订单返回产品(无论如何订购,只要每次调用相同的代码时都是一致的)?或者我应该添加
好吧,假设我有一个用户表,然后是一个记录所有登录尝试的表(Id(计数器)、userId、时间以及是否成功) 现在在我的实体模型中,我希望它成为 2 个关联的一个,包含所有成功尝试的时间和一个包含所有不
我正在努力处理如何最好地跨多个 Visual Studio 项目分解域模型。我正在使用 EntityFramework 4 和企业类型模式来合理化一个可以重用于各种应用程序的系统。最终我想得到一组可以
是否可以使用 POCO 和 EF 处理以下情况?我想要一个 Vehicles 表,有一些 bool 字段,如 IsFast、IsVan、IsTruck,然后有一个 Vehicles 类,有一个 Fas
当我通过传递比较泛型类型 TId 的谓词调用 DbSet.FirstOrDefault() 时,出现以下异常: unable to create a constant value of type 's
我正在使用 EF5 将一些数据从一个数据库迁移到另一个数据库。我通常会使用 SQL 来做这样的事情,但是我需要其他功能(比如在 MembershipProvider 中创建用户)并且希望在 EF 中完
我创建了一个单独的项目并将我的模型复制到这个项目中。我引用了 using System.ComponentModel.DataAnnotations 我的模型定义如下: public class F
上下文: 我有两个类在 SQL 中有两个对应的表。 系统信息 EF 版本: SQL 版本:2012 类 public class Employee { public Employee(
我有一个定义了主从关系的 Entity Framework 对象。详细信息对象集合有一个导航属性。 在稍后的代码中,我尝试使用 AutoMapper 将其中一个主对象映射到数据传输对象。但是,数据传输
有没有办法让这个通用 Context.SalesEntity.Where(t=>t.id==3).Delete(); 有点像 private void DoWork(Expression> predi
来自 NHibernate 背景 - 我有一些审计列来反射(reflect)谁最后编辑了一条记录,以及该编辑发生的时间。 我已经覆盖了 SaveChanges 以寻找一个公共(public)基类,并使
我是 EntityFramework 6 的新手,一直在 POCO 中遇到问题。 以下是我的代码 Domain.cs public abstract class BaseEntity { [K
我刚在单元测试项目中遇到一个问题,坚持认为它需要 EntityFramework 的引用,我确信它不需要它。其他项目正在引用单元测试项目正在引用/测试的项目/扩展方法,并且在不引用 EntityFra
下面的代码应该做什么?在此代码中添加包含表的目的是什么,它应该对与 PersonId 相关的 JobType 进行级联删除,还是只删除具有指定 PersonId 的 Person ? db.tblPe
我试图了解延迟加载在 Entity Framework 6 中是如何工作的。在我看来,延迟加载只适用于 DbContext,其中“to-be-loaded”属性已经存在访问过。请查看下面的示例 - 测
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Entity Framework Validation EntityFramework有什么好的灵活的验证框
我是一名优秀的程序员,十分优秀!