【问题标题】:Datareader already open in the view, complex objectDatareader 已在视图中打开,复杂对象
【发布时间】:2014-10-01 09:10:26
【问题描述】:

我在视图中收到此消息: 已经有一个打开的 DataReader 与此 Connection 关联,必须先关闭它。

这是我的模型:

public class Product
{
    [DisplayName("Id Produit")]
    public int productId { get; set; }

    [DisplayName("Nom du produit")]
    public string productName { get; set; }

    [DisplayName("Lien")]
    public string productLink { get; set; }

    [DisplayName("Categorie")]
    public int categoryId { get; set; }
    [ForeignKey("categoryId")]
    public virtual Category category { get; set; }

     [DisplayName("Tags")]
    public virtual ICollection<Tag> tags { get; set; }
    public Product()
    {
        tags = new HashSet<Tag>();
    }

在视图中这是有效的:

<td>
 @Html.DisplayFor(modelItem => item.categoryId)
</td>

但这不起作用:

<td>
 @Html.DisplayFor(modelItem => item.category.categoryName)
</td>

实际上,如果我尝试在复杂对象中导航,我会收到有关数据读取器的消息。 任何的想法? :s

【问题讨论】:

    标签: asp.net asp.net-mvc code-first datareader


    【解决方案1】:

    实际上我不得不像这样映射:

    modelBuilder.Entity<Product>()
              .HasRequired(a => a.category)
              .WithMany()
              .HasForeignKey(u => u.categoryId);
    

    不知道为什么它解决了问题

    【讨论】:

    • 这不是答案!
    • 需要将这些放在问题部分,以便更好地了解您的要求。
    猜你喜欢
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多