我正在努力解决上述错误。我在这里发现了不同的答案(堆栈溢出),但没有一个能够解决与错误有关的问题。实体框架已经有一个打开的DataReader与这个命令相关联,必须先关闭
我只是在我的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; }
}
}
和类添加剂在产品中占添加剂
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;}
}
}
我在写代码来实现对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
}
错误的第一个foreach循环后出现完全相同,在此声明:
var additives = p.Additives;
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>
你能显示你的连接字符串吗? – Sampath
我编辑了我的帖子并添加了连接。 – elmodai