Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi-tenancy in EF6 with multiple schemas having the same tables

In our system it has become required to provide a multi-tenant solution, where each tenant has the same data structure.

During investigation I came across an article discussing multi-tenancy with EF4.1.

http://romiller.com/2011/05/23/ef-4-1-multi-tenant-with-code-first/

This looks like a sensible solution, but we would prefer to avoid multiple database contexts if possible.

Also, we have a large number of migrations for our current single tenant solution. With EF6, it is possible for a migration to target a specific context and when none is supported, a default is targeted.

I have a couple of quesions here:

  1. Is there a better approach to multi-tenancy when using EF6 other than that specified for EF4?
  2. Is there a better way to handle the migrations?

Any help is much appreciated!

like image 407
Dark Templar Avatar asked Aug 04 '14 12:08

Dark Templar


1 Answers

  1. Context is connecting to a connection string, if the connection string is resolved at runtime, then you are using One dbcontext class with httprequest specific instance. to distinguish httprequest, host name headers can be used.
  2. There is no easy way to handle migration. This is a complex question, but in short, before the end of release 1, i normally create a initial migration with all scripts to provision the database, this is to help the database created after this change it not effected by any migrations down the track. and then add migration every time i need to change it. let me know if more details needed.
like image 119
anIBMer Avatar answered Oct 14 '22 13:10

anIBMer