Solutions for a simple multi tenant web application with entity framework

后端 未结 1 1474
鱼传尺愫
鱼传尺愫 2021-02-04 20:04

I\'m developing a multi-tenant web app (stack: MVC 4 + Entity framework 4.3). My requirements are pretty simple: each tenant has the same UI and CodeBase.

In my databa

1条回答
  •  有刺的猬
    2021-02-04 20:27

    You can do the reflection check and then manually create an expression tree that EF can understand.

    For example:

    int tenantId = 5;
    
    var tenantIdInfo = typeof(TEntity).GetProperty("TenantId");
    
    if (tenantIdInfo != null)
    {
        var entity = Expression.Parameter(typeof(TEntity), "it");
    
        var predicate = (Expression>)Expression.Lambda(
            Expression.Equal(
                Expression.MakeMemberAccess(entity, tenantIdInfo),
                Expression.Constant(tenantId, typeof(int))),
            entity);
    
        query = query.Where(predicate);
    }
    

    0 讨论(0)
提交回复
热议问题