elm*_*dai 3 c# sql-server entity-framework lazy-loading eager-loading
我正在努力解决上面的错误.我在这里发现了不同的答案(堆栈溢出),但它们都没有解决我与错误相关的问题.
我只是在我的ConnectionString中启用MARS但没有成功.
我有一类产品
public class Product
{
public Product()
{
this.Additives = new HashSet<Additive>();
}
public int Id { get; set; }
public string Name { get; set; } // refrigerante
public string CommercialName { get; set; } // nome popular, ex: fanta laranja
public string Brand { get; set; } // marca, ex: Coca-cola
public string Details { get; set; } // composicao, ingredientes
public HalalState HalalState { get; set; } // estado: halal, haram ou desconhecido
public DateTime? LastUpdate { get; set; } // date e hora do registo
public virtual ICollection<Additive> Additives { get; set; } // aditivos
public int ProviderID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
}
和类添加剂代表产品中的添加剂
using System;
using System.Collections.Generic;
namespace Teknowhow.EatHalal.Core.Models
{
public class Additive
{
public Additive()
{
this.Products = new HashSet<Product>();
}
public int Id { get; set; }
public string Key { get; set; } // codigo ex: E130
public string NamePT { get; set; } // nome ex: Acido ascorbico (Vitamina C) em portugues
public string NameEN { get; set; } // nome ex: Acido ascorbico (Vitamina C) em inglês
public string Details { get; set; } // detalhes sobre o aditivo, incluindo.
public HalalState HalalState; // estado: halal, haram ou desconhecido
public DateTime? LastUpdate { get; set; } // date e hora do registo
public virtual ICollection<Product> Products { get; set;}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在编写一个代码来实现ProductRepository上的方法,以便获得具有特定添加剂的产品.
public ICollection<Product> GetProductsByAdditive(string key)
{
var products = context.Products;
var productsAdditives = new List<Product>();
foreach (var p in products)
{
var additives = p.Additives;
foreach (var a in additives)
{
if (a.Key.Equals(key))
productsAdditives.Add(p);
}
}
return productsAdditives.ToList();
//TODO: um Metodo úinico que permite pesquisa por nome em PT e EN e codigo
}
Run Code Online (Sandbox Code Playgroud)
错误发生在第一个foreach循环之后,在此语句上:
var additives = p.Additives;
Run Code Online (Sandbox Code Playgroud)
PS:我正在使用EF 6.我已经堆叠了!请帮忙!
继承人我的联系
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<add name="EatHalal"
providerName="System.Data.SqlClient"
connectionString="Server=192.168.1.150;Database=EatHalal;User Id=user;Password=password;MultipleActiveResultSets=true;"
/>
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
您可以尝试如下所示.
注意:您必须先提取数据然后执行映射,否则您将不会遇到此问题.
原因:当您遍历query(IQueryable)的结果时,您将lazy loading在迭代内触发加载的实体.换句话说,在单个连接上执行多个数据检索命令.
foreach (var p in products.ToList())
{
var additives = p.Additives.ToList();
foreach (var a in additives)
{
if (a.Key.Equals(key))
{
productsAdditives.Add(p);
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8519 次 |
| 最近记录: |