简体   繁体   English

具有多个数据库和泛型的实体框架多租户架构

[英]Entity Framework Multi-Tenant Architecture With Multiple Databases & Generics

I'm needing to build a multi-tenanted application.我需要构建一个多租户应用程序。 The code is completely shared for each company but each company has its own database.每个公司都完全共享代码,但每个公司都有自己的数据库。

Each company accesses the application through a Web API passing their own 'API Key'.每家公司都通过传递他们自己的“API 密钥”的 Web API 访问应用程序。 This key is used to 'look up' the company specific configuration details within the master database's 'GlobalCompanies' table that holds company specific details including the database details and full connection strings.此键用于在主数据库的“GlobalCompanies”表中“查找”公司特定的配置详细信息,该表包含公司特定的详细信息,包括数据库详细信息和完整的连接字符串。

My goal, to set up a new customer, is to simply run some scripts to create a new database and update this 'GlobalCompanies' table with the new database details and everything will just work!我的目标是建立新客户,只需运行一些脚本来创建新数据库并使用新数据库详细信息更新此“GlobalCompanies”表,一切都会正常运行!

So, no configuration information within the web.config file except perhaps the master database holding the configuration details.因此,除了可能包含配置详细信息的 master 数据库之外,web.config 文件中没有配置信息。

The flow would therefore be:因此,流程将是:

  1. Browser calls a Web API method and passes API Key浏览器调用 Web API 方法并传递 API Key
  2. Application uses API Key and determines which database is being used应用程序使用 API 密钥并确定正在使用哪个数据库
  3. Application uses common code but applies database operations to the company-specific database.应用程序使用通用代码,但将数据库操作应用于公司特定的数据库。

I found this article Repository Pattern with Entity Framework 4.1 and Code First which gives me an insight to generics for the Repository pattern but I'm unsure how I would dynamically create and utilise different DBContexts?我发现这篇文章Repository Pattern with Entity Framework 4.1 and Code First它让我深入了解了 Repository 模式的泛型,但我不确定我将如何动态创建和利用不同的 DBContext? (if even that is the correct approach)? (如果即使这是正确的方法)?

I have done this before with ADO.net using different connection strings but ideally I'd like to utilize Entity Framework and generics.我之前使用不同的连接字符串在 ADO.net 上完成了此操作,但理想情况下我想利用实体框架和泛型。

Could someone please point me in the right direction?有人可以指出我正确的方向吗? I couldn't find any specific examples of this architecture.我找不到这种架构的任何具体例子。

EF is the same as what you did with ADO.Net. EF 与您使用 ADO.Net 所做的相同。 You just pass a different connection string to the constructor of the context.您只需将不同的连接字符串传递给上下文的构造函数。 (The default constructor with no parameter will take the connection string from the config file.) The context won't care where the connection string comes from - config file, master database, constructed by the calling code - it will just use it. (没有参数的默认构造函数将从配置文件中获取连接字符串。)上下文不会关心连接字符串来自哪里 - 配置文件,主数据库,由调用代码构造 - 它只会使用它。

Of course, if the connection string is malformed, or points to a db which doesn't contain the expected tables, then there will be problems.当然,如果连接字符串格式错误,或者指向的数据库不包含预期的表,那么就会出现问题。 But I'm sure that you'd expect that.但我相信你会期待的。

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

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