【问题标题】:EntityCommandExecutionException occures when loading embedded entity加载嵌入式实体时发生 EntityCommandExecutionException
【发布时间】:2013-03-28 08:16:38
【问题描述】:

我有一个从我的数据库中获取所有产品的代码:

using (var entities = new DataEntities())
        {
            var products = entities.Products.AsQueryable();
            if (!string.IsNullOrEmpty(nameFilter))
            {
                products = products.Where(o => o.Name.Contains(nameFilter));
            }
            var result = products.Select(ProductBuilder.CreateProductDto);
            return result.ToList();
        }

CreateProductDto 方法:

        public static ProductDto CreateProductDto(this Product product)
    {
        return new ProductDto
        {
            Id = product.Id,
            Name = product.Name,
            IsEnabled = product.IsEnabled,
            KeyPairDto = new KeyPairDto()
            {
                Id = product.KeyPair.Id,
                EncryptedPrivateExponent = product.KeyPair.EncryptedPrivateExponent,
                Modulus = product.KeyPair.Modulus,
                PublicExponent = product.KeyPair.PublicExponent,
            },
        };
    }

它在我同事的机器上运行良好。但我得到 EntityCommandExecutionException 并出现以下内部异常:已经有一个打开的 DataReader 与此命令关联,必须先关闭。尝试访问 product.KeyPair 时。

有趣的是,如果我通过 Debugger 刷新 product.KeyPair - 它加载正常。

【问题讨论】:

    标签: c# sql-server database entity-framework


    【解决方案1】:

    添加

    MultipleActiveResultSets=true
    

    到实体框架连接字符串中连接字符串的提供者部分(即定义数据源、初始目录等的部分)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      相关资源
      最近更新 更多