use*_*375 6 c# entity-framework
我想将 CompanyID 过滤器添加到我的所有实体框架请求中。因为每个用户必须只看到他们的记录。我不想在业务层中添加过滤器 (x=>x.CompanyID == cID) 的所有方法。如何自动添加过滤器要求。
我在 DAL 中的 GetList 方法
public List<TEntity> GetList(Expression<Func<TEntity, bool>> filter)
{
using (var context = new TContext())
{
return filter == null
? context.Set<TEntity>().ToList()
: context.Set<TEntity>().Where(filter).ToList();
}
}
Run Code Online (Sandbox Code Playgroud)
商业
public List<FinanceData> GetAll()
{
return _financeDal.GetList(filter:x=>x.CompanyID==_cID);
}
Run Code Online (Sandbox Code Playgroud)
Sin*_*tfi 12
在Entity Framework Core 2.0 中,您可以使用全局查询过滤器。
仅向一个实体添加过滤器:
public interface IDelete
{
bool IsDeleted { get; set; }
}
public class Blog : IDelete
{
public int BlogId { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public List<Post> Posts { get; set; }
}
public class Post : IDelete
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public bool IsDeleted { get; set; }
public int BlogId { get; set; }
public Blog Blog { get; set; }
}
// Default method inside the DbContext or your default Context
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(builder);
modelBuilder.Entity<Blog>()
// Add Global filter to the Blog entity
.HasQueryFilter(p => p.IsDeleted == false);
modelBuilder.Entity<Post>()
// Add Global filter to the Post entity
.HasQueryFilter(p => p.IsDeleted == false);
}
Run Code Online (Sandbox Code Playgroud)如果你有很多实体,第一种方法不好,使用下面的代码将全局过滤器应用于所有实体(魔术方式):
public static class ModelBuilderExtension
{
public static void ApplyGlobalFilters<TInterface>(this ModelBuilder modelBuilder, Expression<Func<TInterface, bool>> expression)
{
var entities = modelBuilder.Model
.GetEntityTypes()
.Where(e => e.ClrType.GetInterface(typeof(TInterface).Name) != null)
.Select(e => e.ClrType);
foreach (var entity in entities)
{
var newParam = Expression.Parameter(entity);
var newbody = ReplacingExpressionVisitor.Replace(expression.Parameters.Single(), newParam, expression.Body);
modelBuilder.Entity(entity).HasQueryFilter(Expression.Lambda(newbody, newParam));
}
}
}
// Default method inside the DbContext or your default Context
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
modelBuilder.Entity<Blog>();
modelBuilder.Entity<Post>();
builder.ApplyGlobalFilters<IDelete>(e => e.IsDeleted == false);
}
Run Code Online (Sandbox Code Playgroud)查询将是:
exec sp_executesql N'SELECT [x].[BlogId], [x].[Name], [x].[Url]
FROM [dbo].[Blog] AS [x]
WHERE [x].[IsDeleted] = 0'
Run Code Online (Sandbox Code Playgroud)
您建议的所有内容都不适用于以下情况:HasQueryFilter电源,但每个 HTTP 请求。ApplyGlobalFilters/OnModelCreating应用于模型创建一次。但是,如果您使用不同的请求参数重新加载页面 - 它们将不会被考虑以过滤掉DbSet. 如果您添加要GetAll调用的过滤器- 另一个调用 ' Include'-ing 实体将没有此过滤器。我们需要真正的全局机制来DbSet根据特定条件过滤掉s - 每个请求(页面刷新)可能会改变。
您可以IHasCompanyId在此类实体中实现接口。然后将存储库模式实现为:
public class MyRepository<T>
{
public MyRepository(DbContext dbContext, int companyID)
{
if (dbContext == null)
throw new ArgumentNullException("Null DbContext");
DbContext = dbContext;
DbSet = DbContext.Set<T>();
CompanyID = companyID;
}
protected DbContext DbContext { get; set; }
protected int CompanyID { get; set; }
protected DbSet<T> DbSet { get; set; }
// Add filter here
public virtual IQueryable<T> GetAll()
{
if(typeof(IHasCompanyID).IsAssignableFrom(typeof(T)))
return DbSet.Where(x => x.CompanyID == CompanyID);
else
return DbSet;
}
}
Run Code Online (Sandbox Code Playgroud)
并初始化_financeDal为:
var _financeDal = new MyRepository<TEntity>(dbContext, companyID);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15301 次 |
| 最近记录: |