Solutions for a simple multi tenant web application with entity framework -


I am developing a multi-tenant web app (Stack: MVC 4 + Entity Framework 4.3). My expectations are very simple: each tenant has the same UI and CodeBase

In my database there are some tables with a tenantID field (and without any other table).

I have established a very simple generic store:

  public class generated registrar & lt; TENTity & gt; Where tementi: class {internal database. CRMNTTI reference; Internal DbSet & lt; TENTity & gt; DbSet; Internal interval tenantId; Public Generic Repository (Database CRMNTT Reference) {this.context = context; This.dbSet = context.Set & lt; TEntity & gt; (); This.tenantId = 1; }   

I need to "get" the method to filter for my tenant ID. My insert, update, and delete method should also restrict the appropriate TenantId.

My institutions are self-activated POCO classes.

I have considered these solutions:

1- The generic repository should implement the "attendant" interface that defines the TenanID. The problem is that some institutions have not got the tenant ID property. Also, I do not really like to modify the TOI template which I use to generate POCO objects so that they can be applied to my interface. > <-> 2-reflection (but definitely EF can not translate it into SQL statement)

  if (typef (TNTTI) .GetProperty ("TenantId")! = Null) Quay Mr. Kwerikvhaan = (x = & gt; (int) (x.GetType (). GetProperty ( "tenant ID"). GetValue (X, free)) == tenantId;);   

What will you do in my case? If necessary, I am ready to reconsider my architecture.

Thanks, Nikola

You can check the reflection and then manually Form can form tree of expression which EF can understand.

For example:

  int tenantId = 5; Var tenantIdInfo = typeof (TENTity) .GetProperty ("TenantId"); If (tenantIdInfo! = Null) {var entity = Expression.Parameter (type (TENTity), "this"); Expression = (expression & lt; Func & gt; TEntity, bool & gt; & gt;) expression. Lambda (Expression. Aquel (Expression. Meckmember Entry (unit, tenantIdInfo), expression Constant (tenantId, typef (int)), unit); Query = query Where (determination); }    

Comments