EF Core 2.2渴望加载导航属性相关的数据问题

时间:2019-03-03 12:46:36

标签: entity-framework-core ef-core-2.2

我将EF core 2.2与Postgresql和代码优先方法结合使用。 这些是我的课程:

public class SchoolContext : DbContext
{
    public SchoolContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

}

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

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

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

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

我在postgresql中看到了DDL,看起来还不错。 问题在于查询该数据。 我想使用紧急加载,并且此查询不会产生包含相关数据的结果:

        this._context.Blogs
            .Include(blog => blog.Posts)
            .ToList();


SELECT blog."BlogId", blog."Url"
  FROM "Blogs" AS blog
  ORDER BY blog."BlogId"

编辑:该查询首先运行。如果没有记录,它将在此处停止,但是如果有记录,则它将运行以下查询:

SELECT "blog.Posts"."PostId", "blog.Posts"."BlogId", "blog.Posts"."Title"
  FROM "Posts" AS "blog.Posts"
  INNER JOIN (
      SELECT blog0."BlogId"
      FROM "Blogs" AS blog0
  ) AS t ON "blog.Posts"."BlogId" = t."BlogId"
  ORDER BY t."BlogId"

如果我反其道而行之,我会正确,但是令人困惑的是我无法获得所有相关帖子的博客。

this._context.Posts
            .Include(post => post.Blog)
            .ToList();

  SELECT post."PostId", post."BlogId", post."Title", "post.Blog"."BlogId", "post.Blog"."Url"
  FROM "Posts" AS post
  INNER JOIN "Blogs" AS "post.Blog" ON post."BlogId" = "post.Blog"."BlogId"

0 个答案:

没有答案