1

ととを使用Entity Framework 4.1 code firstしていASP.NET MVC 3ます。Razor viewValueInjecter

私のビューモデル:

public class ProductViewModel
{
     public int Id { get; set; }
     public string SKU { get; set; }
     public string Name { get; set; }
     public ICollection<Specification> Specifications { get; set; }
}

モデルクラス:

public class Product : IEntity
{
     public int Id { get; set; }
     public string SKU { get; set; }
     public string Name { get; set; }
     public virtual ICollection<Specification> Specifications { get; set; }
}

商品のリストを返し、各商品をビューモデルにマッピングする必要があるアクションメソッド。

public ActionResult JsonGetProductList()
{
     IEnumerable<Product> productList = productService.GetAll();

     // Mapping
     IList<ProductViewModel> viewModelList = productList.Select(c => new ProductViewModel().InjectFrom(c)).Cast<ProductViewModel>().ToList();
}

次のエラーでマッピング部分にエラーが発生しています。

There is already an open DataReader associated with this Command which must be closed first.

これをどのように修正しますか?

4

1 に答える 1

2

このエラーは、元のクエリのDataReaderが閉じられる前に結果のプロパティを遅延ロードすると表示されます。GetAllの後にToListメソッドを呼び出してクエリを完全に実行するか、MultipleActiveResultSets構成を接続文字列に追加して、次のような複数のDataReaderを許可することができます。

connectionString = "data source = YourServer; Integrated Security = SSPI; Initial Catalog = YourDB; MultipleActiveResultSets = true"

于 2011-11-12T13:12:46.077 に答える