[英]Accessing EF ConnectionString from different project within same solution MVC
In trying to practice separation of concerns, I am taking an ASP.NET MVC project and putting the Models and the DBContext into a separate project within the same solution. 在尝试分离关注点时,我正在使用ASP.NET MVC项目并将Models和DBContext放入同一解决方案中的单独项目中。 Now I have a Project.Web which houses the ViewModels, Controllers and Views and I have a Projects.Entities for the Models and DAL.
现在我有一个包含ViewModels,Controllers和Views的Project.Web,我有一个Projects.Entities用于Models和DAL。 The web.config file that has the ConnectionString attribute is in the Project.Web project.
具有ConnectionString属性的web.config文件位于Project.Web项目中。
Project.Web Project.Web
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=localhost;Initial Catalog=ContosoUniversity1;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Home Controller: 家庭控制器:
public class HomeController : Controller
{
private SchoolContext db = new SchoolContext();
Project.Entities Project.Entities
public class SchoolContext : DbContext
{
public SchoolContext() : base("SchoolContext")
{
}
My issue is that the SchoolContext connectionString name isn't getting picked up by the DBContext because its in the web.config of the other project. 我的问题是,DBContext没有获取SchoolContext connectionString名称,因为它在另一个项目的web.config中。 How do I mitigate this?
我该如何缓解这种情况? Everything works fine when all the MVC components are in the same project.
当所有MVC组件都在同一个项目中时,一切正常。
This is basically how I set up my solutions. 这基本上就是我设置解决方案的方式。 I can let the web project web.config or test project app.config define the connection string.
我可以让web项目web.config或测试项目app.config定义连接字符串。
Data Project 数据项目
public class SchoolContext : DbContext
{
public SchoolContext(string connectionString)
: base(connectionString) { }
}
Web Project 网络项目
web.config web.config中
<configuration>
<connectionStrings>
<add name="SchoolContextDb" connectionString="Data Source=..." />
</connectionStrings>
<appSettings>
<add key="SchoolContext" value="SchoolContextDb" />
</appSettings>
...
</configuration>
usage 用法
string ConnectionString = System.Configuration.ConfigurationManager.AppSettings["SchoolContext"];
SchoolContext db = new SchoolContext("Name=" + ConnectionString);
I use a DI container so I only really look up the connection string once in the application start up code. 我使用DI容器,所以我只在应用程序启动代码中查找连接字符串一次。 But you could make the
ConnectionString
variable global or set in a base controller. 但是您可以将
ConnectionString
变量设置为全局变量或在基本控制器中设置。
By default entry point solution check web.config with connectionstring and it goes checking for reference project. 默认情况下,入口点解决方案使用connectionstring检查web.config并检查参考项目。
<entityFramework> <contexts> <context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity.DAL"> <databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity.DAL" /> </context> </contexts> </entityFramework>
The code I had in the initial question worked like a charm even with the DAL split into the different project. 我在初始问题中的代码就像一个魅力,即使DAL分成不同的项目。 The issue was where the EF Code First Initializer wasn't getting executed, this was because I didn't have the correct assembly in the web.config file when I moved the initializer code to its own project.
问题是EF Code First Initializer没有被执行,这是因为当我将初始化代码移动到它自己的项目时,我在web.config文件中没有正确的程序集。 The modified web.config should look like this (I was missing the .Entities where the DAL is in a project called ContosoUniversity.Entities.
修改后的web.config看起来应该是这样的(我错过了DAL在一个名为ContosoUniversity.Entities的项目中的.Entities。
<entityFramework>
<contexts>
<context type="ContosoUniversity.DAL.SchoolContext, ContosoUniversity.Entities">
<databaseInitializer type="ContosoUniversity.DAL.SchoolInitializer, ContosoUniversity.Entities" />
</context>
</contexts>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.