@Andrej , Based on your description, you want to apply common configuration to all entity types.
You can use some reflection , expression trees and IMutableModel.GetEntityTypes
in your OnModelCreating
method.
Like the following code
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// add your own configuration here
Expression<Func<Entity, bool>> filterExpr = bm => bm.TenantId=="TenantId";
foreach (var mutableEntityType in modelBuilder.Model.GetEntityTypes())
{
// check if current entity type is child of BaseModel
if (mutableEntityType.ClrType.IsAssignableTo(typeof(Entity)))
{
// modify expression to handle correct child type
var parameter = Expression.Parameter(mutableEntityType.ClrType);
var body = ReplacingExpressionVisitor.Replace(filterExpr.Parameters.First(), parameter, filterExpr.Body);
var lambdaExpression = Expression.Lambda(body, parameter);
// set filter
mutableEntityType.SetQueryFilter(lambdaExpression);
}
}
}
Hope this could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.