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.
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:
Any help is much appreciated!
I understand this is old, however it's still widely searched topic.
I have used the following methods successfully with having multi-tenant by schema, using Code-First or DB First using EDMX.
command.CommandText = command.CommandText.Replace("<unwanted-schema-here>", "<your-actual-schema>")under all the *Executing implementations. Then Remove and Add the Interceptor before initializing a new context.
Place your factory method inside a Partial class of your
Private Shared _lschema As String Public ReadOnly Property schema As String = _lschema Public Shared Function GetContext(Optional connString As String = Nothing) As MyContext If connString IsNot Nothing Then Dim conn As EntityConnection = ContextHelper.CreateConnection(Of MyContext)(connString,ByRef _lschema) DbInterception.Remove(New SchemaInterceptor) DbInterception.Add(New SchemaInterceptor(_lschema)) Dim ctx = New MyContext(conn, False) ctx.Configuration.LazyLoadingEnabled = False ctx.Configuration.AutoDetectChangesEnabled = True Return ctx End If Return New MyContext() End Function
IDbModelCacheKeyProvider, this is used in a similar method as the example above. Such as building your connection by calling a method that gets the schema of the connection and returning it.