【问题标题】:Querying related entities with Eager & Explicit loading it doesn't work in EF for ASP.NET MVC CORE使用 Eager & Explicit 加载查询相关实体在 EF for ASP.NET MVC CORE 中不起作用
【发布时间】:2016-12-25 07:39:05
【问题描述】:

我在 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; }
    }

控制器

     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>

局部视图

@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。 ThenIclude (with intellisense) 不显示表格

错误代码: InvalidOperationException:属性表达式 'b => {from Post x in [b].Posts select [x].Person}' 无效。该表达式应表示属性访问:'t => t.MyProperty'。有关包含相关数据的更多信息,请参阅http://go.microsoft.com/fwlink/?LinkID=746393

提前感谢您的疑难解答,我无法解决。

【问题讨论】:

  • EF Core 与 EF6 存在差异和不兼容。对于 EF Core,您应该使用 .Include(b =&gt; b.Posts).ThenInclude(x =&gt; x.Person) - 请参阅 Loading Related Data
  • 我已经尝试过了,但是如上所述,智能感知不显示实体(人)。还是谢谢
  • 嘿嘿,Intellisense的问题。复制/粘贴上面的 - 它会编译和工作:) 见stackoverflow.com/questions/41129970/…
  • 我会的!我现在在家外面。圣诞快乐..

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


【解决方案1】:

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

虽然智能感知不显示相关实体,但在构建应用时它可以工作。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-25
    • 2016-08-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多