实体框架核心过滤器DbSet(Entity Framework Core Filter DbSet)

编程入门 行业动态 更新时间:2024-10-28 20:21:44
实体框架核心过滤器DbSet(Entity Framework Core Filter DbSet)

实体框架核心可以自动过滤DbSet<TEntity>吗? 我正在寻找像 EntityFrameworkCore 那样的实现。 我想在它通过DbSet<TEntity>访问之前自动过滤IQueryable<TEntity> DbSet<TEntity> 。

Is it possible in Entity Framework Core to automatically filter a DbSet<TEntity> of a DbContext? I'm looking to implement something like that just for EntityFrameworkCore. I would like to automatically filter the IQueryable<TEntity> before it's beeing accessed over the DbSet<TEntity>.

最满意答案

你可以看看下面的链接。

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#model-level-query-filters

public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } public int TenantId { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Post>().HasQueryFilter( p => !p.IsDeleted && p.TenantId == this.TenantId ); } }

you can look at the link below.

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#model-level-query-filters

Example

public class BloggingContext : DbContext { public DbSet<Blog> Blogs { get; set; } public DbSet<Post> Posts { get; set; } public int TenantId { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Post>().HasQueryFilter( p => !p.IsDeleted && p.TenantId == this.TenantId ); } }

更多推荐

本文发布于:2023-08-03 08:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385325.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   实体   框架   核心   DbSet

发布评论

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

>www.elefans.com

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