0

我正在使用带有 Postgresql 和代码优先方法的 EF core 2.2。这些是我的课:

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"
4

0 回答 0