[英]Custom where clause extension with default values EF Core 5.0.3
I am adding this on every query and I can't help but think there is a way that I could either do a custom where extension to have it their as default on my queries?我在每个查询上都添加了这个,我不禁想到有一种方法可以做一个自定义的 where 扩展,让它在我的查询中作为默认值? Or am I over-thinking this?
还是我想太多了?
I am talking about isActive
and isDeleted
which is for my soft delete columns.我说的是
isActive
和isDeleted
,它们用于我的软删除列。
I am using EF Core 5.0.3 if makes a difference, to what is avail to me I no it's only one line but I feel that allot of the repetitive code code be cut down.如果有所作为,我正在使用 EF Core 5.0.3,对我有什么用处我不,这只是一行,但我觉得分配的重复代码代码被削减了。 Or at least a cleaner way to enforce it?
或者至少是一种更清洁的方式来执行它? Its on every class that I will be querying.
它在我将要查询的每个 class 上。
public async Task<IActionResult> ClubRoles() {
return View(await _context.ClubUsers.Where(w => w.isActive == true && w.isDeleted == false).ToListAsync();
}
Easy enough with global query filters: https://docs.microsoft.com/en-us/ef/core/querying/filters使用全局查询过滤器足够简单: https://docs.microsoft.com/en-us/ef/core/querying/filters
Your case is textbook usage您的案例是教科书用法
modelBuilder.Entity<Post>().HasQueryFilter(p => !p.IsDeleted);
In case you want to ignore it如果你想忽略它
blogs = db.Blogs
.Include(b => b.Posts)
.IgnoreQueryFilters()
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.