简体   繁体   English

StructureMap-为多租户Web应用程序上的每个数据库初始化实体框架

[英]StructureMap - Initialise Entity Framework for each database on a multitenant web application

We are developing a multi tenanted ASP.NET MVC web application. 我们正在开发一个多租户的ASP.NET MVC Web应用程序。 Each tenant has their own database. 每个租户都有自己的数据库。 Data access is managed by the entity framework. 数据访问由实体框架管理。 StructureMap is being used as the IoC container. StructureMap被用作IoC容器。 The entity framework data context has been setup with http request scope. 实体框架数据上下文已使用http请求范围设置。 For each request the tenant is worked out and StructureMap returns a new instance of the entity framework data context and repository classes. 对于每个请求,都会计算出租户,然后StructureMap返回实体框架数据上下文和存储库类的新实例。

In the start up class of the web app (implementation of IProcessHostPreloadClient) I would like to initialise the entity framework - I would need to loop around and create a new instance of the datacontext for each configured tenant. 在Web应用程序的启动类(IProcessHostPreloadClient的实现)中,我想初始化实体框架-我将需要为每个配置的租户循环并为数据上下文创建一个新实例。 What is the best way of going about this? 最好的方法是什么? StructureMap would return the same data context as it has been setup to cache with http/thread scope. StructureMap将返回与设置为使用http / thread范围缓存的数据上下文相同的数据上下文。

Thanks, Harsha 谢谢,哈莎

If you just want to vary the connection string based on the tenant, the following should work: 如果您只想根据承租人更改连接字符串,则应执行以下操作:

For<Tenant>().Use(c => c.GetInstance<TenantFactory>().CreateTenant());
For<DbContext>().Use(c => new DbContext(
    ConfigurationManager.ConnectionStrings[c.GetInstance<Tenant>().ConnectionName]
        .ConnectionString));

The Tenant class should hold the current tenant connection string name and the TenantFactory would create the appropriate Tenant unsing whatever method you use for determining the connection string. Tenant类应保留当前的租户连接字符串名称, TenantFactory将使用您用于确定连接字符串的任何方法创建适当的Tenant

If it's a more extensive variation, a multi-container approach might be better. 如果变化更大,则多容器方法可能更好。

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

相关问题 每个用户的实体框架多租户 - Entity Framework MultiTenant Per User 使用StructureMap IoC /实体框架时是否关闭数据库连接? - Closing database connection when using StructureMap IoC / Entity Framework? 具有分层Web应用程序的实体框架 - Entity Framework with a layered web application 实体框架-从ASP.NET Web应用程序中的代码迁移数据库 - Entity Framework - Migrate Database from Code in ASP.NET Web Application 实体框架:如果重新安装了应用程序,则删除数据库 - Entity Framework: Drop database if application is reinstalled 在Web应用程序中使用structureMap示例包 - Using structureMap sample package in web application IoC的StructureMap使用4.5 .net框架,而不是MVC 3应用程序中的4.0框架 - StructureMap for IoC works with 4.5 .net framework, not with 4.0 framework in MVC 3 application 实体框架 + 生命周期 + StructureMap + 工作单元 + Windows Forms? - Entity Framework + Life Time + StructureMap + Unit Of Work + Windows Forms? 如何在ASP .NET MVC 5.1 Web应用程序的实体框架中使用哈希ID,以避免可预测地链接到数据库中的记录 - How to use hashed IDs within Entity Framework in ASP .NET MVC 5.1 web application to avoid predictible links to records in database 实体框架创建与模型中每个类对应的单独数据库 - Entity framework creating separate database corresponding to each class in model
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM