在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”。

public class User
{
public long UserId { get; set; }
public string UserName { get; set; }
public string UserPwd { get; set; }
public DateTime UserCreateDate { get; set; }
public DateTime LastLoginDate { get; set; }
public DateTime LastActivityDate { get; set; }
public int UserRoleId { get; set; } public virtual UserProfile UserProfile { get; set; }
}
public class UserProfile
{
[Key, ForeignKey("User")]
public long UserId { get; set; }
public int Gender { get; set; }
public string NickName { get; set; }
public string Signature { get; set; }
public string Intro { get; set; }
public DateTime? Birth { get; set; }
public string Location { get; set; }
public string Website { get; set; }
public string Qq { get; set; }
public string WeiBo { get; set; }
public string Medals { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
public bool IsSendEmail { get; set; } public virtual User User { get; set; }
}

如果有User的对象,想访问自己的UserProfile里的属性值,直接访问会出现异常。

例如

@p.UserName   //ok
@p.UserProfile.Intro   //出现异常

异常为There is already an open DataReader associated with this Command which must be closed first。

导航属性UserProfile采用的是延迟加载(惰性加载),在访问它的时候才加载。在读对象自己的属性值时已经打开了一个DataReader,然后在访问到延迟加载的导航属性时,又需要DataRead来读取这个导航属性中的各个属性值,而前一个DataReader还没有关闭。

解决的办法是采用多数据结果集。

多数据结果集(Multiple Active Result Sets,简称MARS)-它允许在单个连接上执行多重的数据库查询或存储过程。这样的结果是,你能够在单个连接上得到和管理多个、仅向前引用的、只读的结果集。可以在一个Command对象上同时打开多个DataReader,节约数据库联接所耗费的服务器资源。

在数据库的连接字符串上添加MultipleActiveResultSets=true

<add name="CRGDatabase" connectionString="Data Source=./;Initial Catalog=DB;uid=User;pwd=Pwd;MultipleActiveResultSets=true;" providerName="System.Data.SqlClient"/>

在实体对象中访问导航属性里的属性值出现异常“There is already an open DataReader associated with this Command which must be closed first”的更多相关文章

  1. 关于MultipleActiveResultSets属性导致的There is already an open DataReader associated with this Command which must be closed first的解决方法

    执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话,会得到一个错误提示:There is already an open Dat ...

  2. ng-repeat 嵌套访问父作用域里的属性

    在一个项目中,需要嵌套循环输出一个二维表的里的数据 数据结构 [ { id:1, list:[ { id:1, name:'li' } ] }, { id:2, list:[ { id:1, name ...

  3. Drools规则引擎-如果判断某个对象中的集合是否包含指定的值

    规则引擎集合相关处理 在实际生产过程中,有很多关于集合的处理场景,比如一个Fact对象中包含有一个集合,而需要判断该集合是否包含某个值.而Drools规则引擎也提供了多种处理方式,比如通过from.c ...

  4. C#获取EF实体对象或自定义属性类的字段名称和值

    在年前上班的时候遇到了一个问题是这样描述的:我前台设计一个页面,是标签和文本框,当用户修改了哪个文本框的值,将该修改前的值.修改后的值,该值对应的字段,该值对应的行id获取到保存到数据库的某张表里.现 ...

  5. flask学习(十):模板中访问模型和字典的属性

    访问模型中的属性或者是字典,可以通过{{params.property}}的形式,或者是使用{{params['age']}}这样的形式

  6. 当调用对象中不存的方法、属性时,__getattr__的应用场景

    一.Python中创建类和实例的调用顺序 new(cls) 创建对象前调用,如果类中没定义,会一直向父类找,直到object的 new 方法创建类.cls代表类本身 init(self) 创建类实例后 ...

  7. 【ASP.NET】DataContract序列化,反序列化对象中包含用接口声明的属性时的处理方法

    为此对象添加KnownType属性(Attribute).类型为用接口声明的属性(Property)的所有可能类型.  示例如下: public interface IKey { [DataMembe ...

  8. 自定义T4模板去掉实体对象中的下划线

            在EF Power Tool 默认使用的T4模板中,如果数据库表有下划线,那么生成的实体也有下划线,但是我们实际使用的过程中,是不希望有下划线的,要解决这个问题,可以自定义这个T4模板 ...

  9. .netcore linq更改list实体对象中的字段 批量条件

    List<cs_orders> orderlist = _ordersRepository.WhereLoadEntityEnumerable(p => p.order_status ...

随机推荐

  1. 让Linux修改IP、DNS等可以更简单

    修改IP: 可以用 netconfig,可惜每次都得输入完整的IP.掩码.网关和DNS. 不如直接 vi /etc/sysconfig/network-scripts/ifcfg-eth0 再 /et ...

  2. Linux下Oracle常见安装错误[Z]

    #./runInstaller之后出现如下的错误信息: RedHat AS5 x86上安装Oracle1020 Exception in thread "main" java.la ...

  3. C趣味100道之58.拉丁方的一些想法。

    题目如上. 思路(未写) 完整代码如下: #include<iostream> #include<queue> #include<math.h> using nam ...

  4. leetcode Same Tree python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  5. 出现"无法连接synaptics定点装置驱动程序"

    "开始“--”运行“--regedit(打开注册表)--依次打开 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVERSION/R ...

  6. Java学习之道:Java项目打包发布

    Java项目打包发布 如果只想发布为一个可执行的jar包,使用eclipse的Export功能就可以了 使用eclipse的Export功能,将项目中的所有package打包为一个pet.jar文件, ...

  7. textarea 输入框限制字数

    在textarea标签中,只需要设置maxlength=”***”即可,但是在textarea标签中,IE9及IE9以下浏览器是不支持的,IE10.IE11则支持,估计后续的版本应该都会支持. 现在来 ...

  8. <a>标签中 href="/" 和 hideFocus="true"

    一:href="/" 连接到根目录 这是相对路径的根目录表示方法 可以写为 http://class.qq.com/">腾讯精品课 (绝对路径写法) 也可以为 腾讯 ...

  9. JS知识点摘记(一)

    JavaScript:基于对象和事件的脚本语言 特点: 安全性:不允许直接访问本地硬盘,可做的就是信息的动态交互 跨平台性:只要可以解析JS的浏览器就可执行,与平台无关 JavaScript与Java ...

  10. Apache commons (Java常用工具包)简介

    Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...