具有多个数据库的实体框架多租户架构泛型 [英] Entity Framework Multi-Tenant Architecture With Multiple Databases & Generics

查看:69
本文介绍了具有多个数据库的实体框架多租户架构泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个多租户应用程序。每个公司都完全共享该代码,但是每个公司都有自己的数据库。



每个公司都通过传递自己的 API密钥的Web API访问该应用程序。此键用于在主数据库的 GlobalCompanies表中查找公司特定的配置详细信息,该表包含公司特定的详细信息,包括数据库详细信息和完整的连接字符串。



我的目标是建立新客户,只需运行一些脚本即可创建新数据库,并使用新数据库详细信息和所有内容更新此 GlobalCompanies表会工作!



因此,除了可能包含配置详细信息的主数据库之外,web.config文件中没有任何配置信息。



流将因此为:


  1. 浏览器调用Web API方法并传递API密钥

  2. 应用程序使用API​​密钥并确定正在使用哪个数据库

  3. 应用程序使用通用代码,但将数据库操作应用于公司特定的数据库。

我发现本文带有Entity Framework 4.1和代码优先的存储库模式,这使我对存储库模式的泛型有深刻的了解,但是我不确定如何动态创建和利用不同的存储库DBContexts? (即使这是正确的方法)?



我之前在ADO.net上使用不同的连接字符串完成了此操作,但理想情况下,我想使用实体框架和泛型。



有人可以指出正确的方向吗?我找不到该架构的任何特定示例。

解决方案

EF与ADO.Net相同。您只需将其他连接字符串传递给上下文的构造函数。 (没有参数的默认构造函数将从配置文件中获取连接字符串。)上下文将不关心连接字符串的来源-配置文件,主数据库(由调用代码构造)-它将仅使用它。 / p>

当然,如果连接字符串格式错误,或指向不包含预期表的db,则将出现问题。但我确定您会期望的。


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'. 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.

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!

So, no configuration information within the web.config file except perhaps the master database holding the configuration details.

The flow would therefore be:

  1. Browser calls a Web API method and passes API Key
  2. Application uses API Key and determines which database is being used
  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? (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.

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. 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.

这篇关于具有多个数据库的实体框架多租户架构泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆