简体   繁体   English

EF6 中的多租户具有多个具有相同表的架构

[英]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.在调查期间,我看到了一篇讨论 EF4.1 多租户的文章。

http://romiller.com/2011/05/23/ef-4-1-multi-tenant-with-code-first/ 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.使用 EF6,迁移可以针对特定上下文,当不支持时,将针对默认值。

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?除了为 EF4 指定的方法之外,在使用 EF6 时是否有更好的多租户方法?
  2. Is there a better way to handle the migrations?有没有更好的方法来处理迁移?

Any help is much appreciated!任何帮助深表感谢!

  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.上下文正在连接到连接字符串,如果连接字符串在运行时解析,那么您使用的是一个带有 httprequest 特定实例的 dbcontext 类。 to distinguish httprequest, host name headers can be used.为了区分httprequest,可以使用主机名头。
  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.这是一个复杂的问题,但简而言之,在第 1 版结束之前,我通常会使用所有脚本创建初始迁移以配置数据库,这是为了帮助在此更改后创建的数据库不受任何迁移的影响. and then add migration every time i need to change it.然后每次我需要更改它时添加迁移。 let me know if more details needed.如果需要更多详细信息,请告诉我。

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.我已经成功地使用以下方法通过模式使用代码优先或使用 EDMX 的 DB First 来实现多租户。

  1. DB First (EDMX) - Edit the three sections that make up your EDMX; DB First (EDMX) - 编辑组成 EDMX 的三个部分; SSDL, CSDL, and CS. SSDL、CSDL 和 CS。
    • Remove any references to the Schema by replacing the schema name with nothing.通过将架构名称替换为空来删除对架构的任何引用。
  2. Implement IDbCommandInterceptor and include command.CommandText = command.CommandText.Replace("<unwanted-schema-here>", "<your-actual-schema>") under all the *Executing implementations.实现IDbCommandInterceptor并在所有*Executing实现下包含command.CommandText = command.CommandText.Replace("<unwanted-schema-here>", "<your-actual-schema>") Then Remove and Add the Interceptor before initializing a new context.然后在初始化新上下文之前删除并添加拦截器。

Example: Place your factory method inside a Partial class of your DbContext .示例:将工厂方法放在DbContext的 Partial 类中。

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
  1. Work to build your own model dynamically, using IDbModelCacheKeyProvider , this is used in a similar method as the example above.使用IDbModelCacheKeyProvider动态构建您自己的模型,这在与上述示例类似的方法中使用。 Such as building your connection by calling a method that gets the schema of the connection and returning it.例如通过调用获取连接模式并返回它的方法来构建连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM