c# - ef core include 总是创建左连接
问题描述
我想按导航属性过滤数据,所以我希望“包含”生成“内部联接”查询。正如其他问题的答案所说,我将属性设置为“必需”,但它不起作用。
public class Blog
{
public int BlogId { get; set; }
public string Url { get; set; }
public int? Rating { 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 int Rating { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>()
.HasMany(b => b.Posts)
.WithOne(p => p.Blog)
.HasForeignKey(p => p.BlogId)
.OnDelete(DeleteBehavior.NoAction).IsRequired();
}
using (var context = new BloggingContext())
{
var blogs = context.Blogs
.Include(blog => blog.Posts.Where(blog => blog.Content == "xxxx"))
.ToList();
}
生成的sql:</p>
SELECT [b].[BlogId], [b].[OwnerId], [b].[Rating], [b].[Url], [t].[PostId], [t].[AuthorId], [t].[BlogId], [t].[Content], [t].[Rating], [t].[Title]
FROM [Blogs] AS [b]
LEFT JOIN (
SELECT [p].[PostId], [p].[AuthorId], [p].[BlogId], [p].[Content], [p].[Rating], [p].[Title]
FROM [Posts] AS [p]
WHERE [p].[Content] = N'xxxx'
) AS [t] ON [b].[BlogId] = [t].[BlogId]
ORDER BY [b].[BlogId], [t].[PostId]
预期的sql:</p>
SELECT [b].[BlogId], [b].[OwnerId], [b].[Rating], [b].[Url], [t].[PostId], [t].[AuthorId], [t].[BlogId], [t].[Content], [t].[Rating], [t].[Title]
FROM [Blogs] AS [b]
INNER JOIN (
SELECT [p].[PostId], [p].[AuthorId], [p].[BlogId], [p].[Content], [p].[Rating], [p].[Title]
FROM [Posts] AS [p]
WHERE [p].[Content] = N'xxxx'
) AS [t] ON [b].[BlogId] = [t].[BlogId]
ORDER BY [b].[BlogId], [t].[PostId]
版本:ef 核心 5.0
解决方案
推荐阅读
- docker - 使用 Gitlab CI 构建和部署?
- django - 是否可以从 MySQL 数据库中提取 600,000 行并将其显示为邮递员中的 JSON 数据?
- c++ - 局部范围内的常量命名空间
- javascript - 如何向包含当前 URL 的元素添加类
- json - Jenkinsfile 中的嵌套 JSON 布尔问题
- mysql - 如何使用闪存驱动器在没有网络的两台本地主机上共享/使用 MySQL 数据库?
- ruby-on-rails - 了解“LoadError:无法自动加载常量”
- .net - FirebaseException:Javax.Net.Ssl.SSLPeerUnverifiedException 尝试从 Xamarin 应用程序连接到 Firebase 时
- javascript - index.js 不会触发 JS 文件中的函数单击 evt
- javascript - 我的网页上的暗模式按钮有问题。无论我做什么,它总是出错,就好像它是一个无限循环