实体框架核心过滤器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 ); } }更多推荐
发布评论