用Eager& amp;查询相关实体显式加载它在EF for ASP.NET MVC CORE中不起作用

时间:2016-12-25 07:39:05

标签: c# asp.net asp.net-mvc entity-framework .net-core

我在 VS 2015 .net MVC应用程序中尝试了这一点,它运行正常,没有任何问题。

相反,尝试使用DOTNET CORE 1.x测试相同的内容,我无法获得2 ^级别的导航属性。你有这样的情况,或者你知道如何解决?

数据库类

 public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        public List<Post> Posts { get; set; }
    }

    public class Post
    {
        public int PostId { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }

        public Person Person { get; set; }  

        public Blog Blog { get; set; }
    }

    public class Person
    {
        public int PersonId { get; set; }
        public string Username { get; set; }   // I need to show this in the partial view
        public string Action { get; set; }
    }

CONTROLLER

     public async Task<IActionResult> Details(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }


        Blog blog = await _context.Blogs
            .Include(b=>b.Posts)
            .Include(b=>b.Posts.Select(x=>x.Person))    
            .FirstOrDefaultAsync(b => b.BlogId == id);

            //Then.Include  don't show property!


        if (blog == null)
        {
            return NotFound();
        }

        return View(blog);
    }

查看

@model MyProj.Models.Blog

@{
    ViewBag.Title = "Details";
}

<h2>Details</h2>

<div>
    <h4>Blog</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Url)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Url)
        </dd>

    </dl>
</div>
<p>
    @Html.ActionLink("Edit", "Edit", new { id = Model.BlogId }) |
    @Html.ActionLink("Back to List", "Index")
</p>
<br />
<div>
    @Html.Partial("_posts", this.Model.Posts)
</div>

PARTIAL VIEW

@model IEnumerable<MyProj.Models.Post>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Title)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Content)
        </th>
        <th>
            Person
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Title)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Content)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Person.Username)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PostId }) |
            @Html.ActionLink("Details", "Details", new { id=item.PostId }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.PostId })
        </td>
    </tr>
}

</table>

在我的情况下,我无法在显示帖子列表的部分中获取属性Person.Username。然后包括(使用intellisense)不显示表格

错误代码: InvalidOperationException:属性表达式&#39; b =&gt; {来自[b]中的帖子x .Posts选择[x] .Person}&#39;无效。表达式应代表属性访问权限:&#39; t =&gt; t.MyProperty&#39 ;.有关包含相关数据的更多信息,请参阅http://go.microsoft.com/fwlink/?LinkID=746393

提前感谢您排除故障,我无法弄明白。

1 个答案:

答案 0 :(得分:4)

我试图解决这个问题大约20个小时,以了解为什么它不起作用.....最后它是哥伦布的蛋。感谢Ivan Stoev的评论。

虽然intellisense没有显示相关实体,但在构建应用程序时,它可以正常工作。

 Blog blog = await _context.Blogs
            .Include(b=>b.Posts)
                .ThenInclude(p=>p.Person)  
            .FirstOrDefaultAsync(b => b.BlogId == id);