Virtual Navigation Properties and Multi-Tenancy

后端 未结 2 618
失恋的感觉
失恋的感觉 2020-12-18 10:19

I have a standard DbContext with code like the following:

 public DbSet Interests { get; set; }
 public DbSet Users         


        
2条回答
  •  囚心锁ツ
    2020-12-18 10:45

    Just wanted to offer an alternative approach to implementing multi-tenancy, which is working really well in a current project, using EF5 and SQL 2012. Basic design is (bear with me here...):

    1. Every table in the database has a column (ClientSid binary, default constraint = SUSER_SID()) and is never queried directly, only ever via a dedicated view
    2. Each view is a direct select over the table with WHERE (ClientSid = SUSER_SID()) but doesn't select the ClientSid (effectively exposing the interface of the table)
    3. EF5 model is mapped to the VIEW, not the TABLE
    4. The connection string is varied based on the context of the tenant (user / client whatever multi-tenant partition requirement may be)

    That's pretty much it - though it might be useful to share. I know it's not a direct answer to your question, but this has resulted in basically zero custom code in the C# area.

提交回复
热议问题