简体   繁体   English

从同一解决方案MVC中的不同项目访问EF ConnectionString

[英]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并检查参考项目。

  1. with this code snippet, its not getting more details, you getting any error. 使用此代码段,它没有获得更多详细信息,您收到任何错误。
  2. can you verify if you change dbcontext name what error you getting. 你可以验证你是否更改dbcontext名称你得到了什么错误。 by default if it not set then it taking class name define with dbcontext class. 默认情况下,如果未设置,则使用dbcontext类定义类名。
  3. verify ... 验证...
  4. I hope you already set settings in config file. 我希望你已经在配置文件中设置了设置。 verify context and namespace 验证上下文和命名空间

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

相关问题 控制台项目可以重用MVC3 EF4项目中的代码(在同一解决方案中)吗? - Can a console project reuses code from a MVC3 EF4 project (in same solution)? 从同一解决方案中运行第二个项目 - Run a second project from within the same solution 在同一解决方案中的不同测试项目中引用项目中的 C# 命名空间 - Refer a C# namespace in a Project from a different Testing-Project within the same solution 在同一解决方案中从Winform项目访问excel项目 - Accessing an excel Project from A Winform Project in same Solution 如何在同一解决方案中从另一个项目获取App.Config - How do I get the App.Config from a different project within the same solution 在同一解决方案中从其他命名空间访问活动对象 - Accessing Live Objects From Other Namespace Within Same Solution 在同一解决方案中的不同项目中获取Webservice WSDL - Get Webservice WSDL in different project within same solution 从其他项目访问listBox时,但在同一解决方案中,值未显示? - Values are not showing when accessing listBox from other project but in the same solution? 从相同解决方案C#的另一个项目访问资源文件 - Accessing Resource files from another project of same solution C# EF 4.1-从不同计算机上的同一应用访问数据库时出现问题 - EF 4.1 - Problem accessing DB from same app on different machines
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM