Asp.net mvc ICollection中的对象未与要查看的模型一起传递

Asp.net mvc ICollection中的对象未与要查看的模型一起传递,asp.net-mvc,razor,ef-code-first,Asp.net Mvc,Razor,Ef Code First,我正在尝试创建一个可以有评论的新闻网站。我一直在遵循pluralsight.com的指南,也一直在遵循指南中的内容,但当我在运行时调试并查看模型内部的内容时,不会包含注释 我的数据库类: public class ProjectDB { public DbSet<ContentNode> ContentNodes{ get; set; } public DbSet<Comment> Comments { get; set; }

我正在尝试创建一个可以有评论的新闻网站。我一直在遵循pluralsight.com的指南,也一直在遵循指南中的内容,但当我在运行时调试并查看模型内部的内容时,不会包含注释

我的数据库类:

public class ProjectDB {
        public DbSet<ContentNode> ContentNodes{ get; set; }
        public DbSet<Comment> Comments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<ContentNode>()
                .Property(n => n.ID).HasColumnName("NodeID");
            modelBuilder.Entity<ContentNode>()
                .HasMany(node => node.Comments)
                .WithRequired(comment => comment.Node);
        }
}
最后是视图本身:

@model Project.Models.ContentNode

@{
    ViewBag.Title = "GetByID";
}

<h2>GetByID</h2>

<fieldset>
    <legend>News</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Title)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Title)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Body)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Body)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.dateCreated)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.dateCreated)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

    @foreach (var item in Model.Comments)
    {
        @Html.Partial("_Comment",item)
    }

@Html.ActionLink("New comment", "Create", "Comment", new { NodeID = Model.ID }, null)
@model Project.Models.ContentNode
@{
ViewBag.Title=“GetByID”;
}
GetByID
新闻
@DisplayNameFor(model=>model.Title)
@DisplayFor(model=>model.Title)
@DisplayNameFor(model=>model.Body)
@DisplayFor(model=>model.Body)
@DisplayNameFor(model=>model.dateCreated)
@DisplayFor(model=>model.dateCreated)

@ActionLink(“编辑”,“编辑”,新的{id=Model.id})|
@ActionLink(“返回列表”、“索引”)

@foreach(Model.Comments中的var项) { @Html.Partial(“_Comment”,项目) } @ActionLink(“新建注释”、“创建”、“注释”、新建{NodeID=Model.ID},null)
我已经看了很多例子,我已经看了十几遍视频指南,看看我做错了什么。我提出的唯一解决方案是将NodeContent表与Comments表连接起来,并将其投影到另一个模型中,但从我收集的数据来看,这不应该是必需的


非常感谢您的帮助。

这很可能是由于实体框架默认启用了延迟加载。您可以使用
First
方法而不是
Single
方法来明确强制运行查询:

var model = (from f in _db.ContentNodes
             where f.dateCreated.Year == year &&
                   f.dateCreated.Month == month &&
                   f.dateCreated.Day == day &&
                   f.Title == title &&
                   f.ContentType.ID == 1
             select f).First();
return View(model);

没有解决这个问题,我已经尝试过在没有LINQ的情况下这样做:“var model=_db.ContentNodes.Single(r=>r.Title==Title);”但那也不管用,非常管用!非常感谢你的帮助。
@model Project.Models.ContentNode

@{
    ViewBag.Title = "GetByID";
}

<h2>GetByID</h2>

<fieldset>
    <legend>News</legend>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Title)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Title)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.Body)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.Body)
    </div>

    <div class="display-label">
         @Html.DisplayNameFor(model => model.dateCreated)
    </div>
    <div class="display-field">
        @Html.DisplayFor(model => model.dateCreated)
    </div>
</fieldset>
<p>
    @Html.ActionLink("Edit", "Edit", new { id=Model.ID }) |
    @Html.ActionLink("Back to List", "Index")
</p>

    @foreach (var item in Model.Comments)
    {
        @Html.Partial("_Comment",item)
    }

@Html.ActionLink("New comment", "Create", "Comment", new { NodeID = Model.ID }, null)
var model = (from f in _db.ContentNodes
             where f.dateCreated.Year == year &&
                   f.dateCreated.Month == month &&
                   f.dateCreated.Day == day &&
                   f.Title == title &&
                   f.ContentType.ID == 1
             select f).First();
return View(model);
var model = _db.ContentNodes
    .Include(f => f.Comments) // eager load the child collection
    .Single(f => f.dateCreated.Year == year 
         && f.dateCreated.Month == month
         && f.dateCreated.Day == day 
         && f.Title == title 
         && f.ContentType.Id == 1);