MVC 3具有多个数据库连接和web.config应用程序设置

时间:2012-09-24 15:01:33

标签: asp.net-mvc-3 c#-4.0 entity-framework-4

有没有人知道如何使用Entity Framework使用多个数据库连接和多个web.config appSettings来获取MVC 3应用程序?

2 个答案:

答案 0 :(得分:1)

子文件夹(site1,site2等)是虚拟目录还是控制器名称,您可以将它们的名称用于不同的连接字符串。

仅在一个web.config文件中使用多个连接字符串,并通过请求的URL虚拟目录名称(基于命名约定)检索它们。

在web.config:

<connectionStrings>
    <add name="site1" connectionString="data source=BLAHBLAH1" />
    <add name="site2" connectionString="data source=BLAHBLAH2" />
</connectionStrings>

并在代码中:

using System.Configuration;
...
var currentLocationName = HttpRequest.ApplicationPath; // "site1, site2 etc."
string conn = ConfigurationManager.ConnectionStrings[currentLocationName].ConnectionString;
//set the correct connection string for your objectContext (Entity Framework)
return new Entities(conn);

答案 1 :(得分:0)

如果你有7个网站,你还有7个web.config文件 - 每个都指向正确的数据库。