.NET 核心 2 EF 包括

编程入门 行业动态 更新时间:2024-10-15 18:27:59
本文介绍了.NET 核心 2 EF 包括的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用新的 核心和 EF.

I am using new core and EF.

我需要有关 include linq 命令的帮助.我有一些 1:N 模型,如果集合包含一些标记为已删除的数据,我不想包含它们.

I need help with include linq command. I have some 1:N models and if the collection contais some data marked like deleted I do not want to include them.

怎么做?

var company = await _context.Company .Include(y => y.Administrators) .Include(y => y.CompanyPartTimers) .Include(z => z.WorkPlaces) .Include(z => z.Requirements) .FirstAsync(x => x.Id == id);

如果我添加条件

.Include(z => z.WorkPlaces).Where(x=>x.WorkPlaces.Where(x=>!x.IsDeleted))

它不起作用.如何正确书写?

It doesn't work. How to write this correctly?

接下来是我有 IDeletable 接口,如果我有一些自定义的 linq 表达式并且可以为 ex 做会更好.

Next thing is I have IDeletable Interface and it would be better if I had some custom linq expression and could do for ex.

.Include(z => z.WorkPlaces).GetNonDeleted()

有人知道怎么做吗?我试过这样的事情

Does anyone know how to do it? I tryed something like this

public static class LinqExtension { public static IEnumerable<T> GetActive<T>(this IEnumerable<T> source) where T : class, IDeletable { return source.Where(x => x.IsDeleted); } }

谢谢各位.

推荐答案

您可以在 DbContext 中配置查询过滤器.

You can configure a Query Filter in your DbContext.

modelBuilder.Entity<Administrator>() .HasQueryFilter(admin => !EF.Property<boolean>(admin, "IsDeleted"));

应该做的伎俩

参考链接:docs.microsoft/en-us/ef/core/querying/filters

更多推荐

.NET 核心 2 EF 包括

本文发布于:2023-11-16 23:02:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:核心   NET   EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!