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

    在实体对象中访问导航属性里的属性值出现异常“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"/>

  • 相关阅读:
    JavaScript是如何工作的:深入类和继承内部原理 + Babel和TypeScript 之间转换
    JavaScript的工作原理:解析、抽象语法树(AST)+ 提升编译速度5个技巧
    如何保证MongoDB的安全性?
    SQLSERVER 中sp_who, sp_who2和sp_who3(转载)
    exec sp_spaceused如何只返回一个结果集(转载)
    c#静态构造函数 与 构造函数 你是否还记得?(转载)
    如何让.NET Core支持GB2312和GBK
    C#中用HttpWebRequest中发送GET/HTTP/HTTPS请求 (转载)
    ASP.NET Core读取AppSettings (转载)
    使用自定义端口连接SQL Server的方法(转载)
  • 原文地址:https://www.cnblogs.com/brown-birds/p/4062304.html
Copyright © 2011-2022 走看看