Apply common configuration to all entity types ef core 5.0

Andrej 221 Reputation points
2021-12-10T07:38:41.483+00:00

I have entities derived from a base entity in my application
Base entity:

    public abstract class Entity
    {
        public virtual int Id { get; protected set; }

        public virtual string? TenantId { get; protected set; }
    }

Derived entities:

    public class DerivedEntity : Entity
    {
         //some fields
    }

I want to apply global query filter for each entity

modelBuilder.Entity<DerivedEntity1>().HasQueryFilter(e => e.TenantId == "TenantId");
modelBuilder.Entity<DerivedEntity2>().HasQueryFilter(e => e.TenantId == "TenantId");
modelBuilder.Entity<DerivedEntity3>().HasQueryFilter(e => e.TenantId == "TenantId");

But there are many entities in the application. And can forget to apply the filter to new entities.

I would be grateful for your recommendations.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,150 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,223 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-12-10T09:51:12.5+00:00

    @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.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful