Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-Tenant Application and Entity Framework

I am building a multi-tenant application with shared database and shared schema approach. So as per the approach i have "Tenant_Id" column in each of my table. So is there any way to automatically attach a where clause in every query...

like image 281
aforank Avatar asked May 20 '12 12:05

aforank


1 Answers

You can achieve this using a wrapper around your DbContext, and override each collection of entities with a where clause.

public class WrapperContext : YourDBContext
{

  public override DbSet<YourEntitity> YourEntities
  {
    get
    {
      return base.YourEntities.Where(t => t.Tenant_Id == someId);
    }
    set
    {
       base.YourEntities = value;
    }
  }      
}
like image 105
Jayantha Lal Sirisena Avatar answered Sep 30 '22 11:09

Jayantha Lal Sirisena