简体   繁体   English

连接字符串-N层应用

[英]Connection String - N-Tier Application

I have had a dig around for an answer to this issue but everything previously asked seems to be a complex variation on this and doesn't answer the question so I thought I should just ask. 我一直在寻找这个问题的答案,但是先前要求的一切似乎都是对此的复杂变化,并且没有回答该问题,因此我认为我应该问一下。

I am developing a three tier application with the following..... 我正在开发以下三层应用程序:

  • DAL - Data Access Layer using Entity Framework DAL-使用实体框架的数据访问层
  • BLL - Business Logic Layer BLL-业务逻辑层
  • Web App - MVC Web Application Web应用程序-MVC Web应用程序

I have created an Entity Framework model, repository classes and my connection string is in the DAL App.Config file. 我已经创建了一个实体框架模型,存储库类,并且我的连接字符串在DAL App.Config文件中。 I have now created my first class in the BLL and it references the DAL. 我现在在BLL中创建了我的第一个类,它引用了DAL。 When trying to test the following, very basic method I get an error relating to a missing connection string in the BLL. 当尝试测试以下非常基本的方法时,我收到与BLL中缺少连接字符串有关的错误。

public static List<DAL.Item> getItems() {

        List<DAL.Item> result = new List<Item>();

        DAL.Repositories.ItemRepository myRepository = new    DAL.Repositories.ItemRepository();
        result = myRepository.GetAll().ToList();

        return result;

    }

Why is the BLL looking for the connection string? 为什么BLL在寻找连接字符串? Am I missing something really obvious here? 我在这里真的错过了什么吗?

If I need to include the connection string across multiple layers, which defeats the purpose of the n-tier structure, then how is the best way to do this? 如果我需要跨多个层包含连接字符串,这会破坏n层结构的目的,那么最好的方法是什么? Can anyone shed some light on this for me? 谁能为我阐明一下?

您正在新建DAL的实例,该实例将尝试查看您的配置文件(由于作用域位于业务层/单元测试中而未找到。您应该做的是查看控制反转(IoC )并将DAL注入到业务逻辑层中,然后您就可以模拟出不会尝试实际访问配置文件/数据库等的DAL。

原样将连接字符串属性从app.config复制到web.config连接字符串部分...此问题将解决

The connection string should be on your presentation layer (mvc project in your case). 连接字符串应在您的表示层上(在您的情况下为mvc项目)。 Just add the entry on the connectionStrings node in web.config and read it by the System.Configuration on the data layer. 只需在web.config中的connectionStrings节点上添加条目,然后由System.Configuration在数据层上读取它即可。

define in web.config a connection string 在web.config中定义一个连接字符串

<connectionStrings>
    <add name="ConnectionStringName" connectionString="Data Source=.\SQLEXPRESS; Database=DataBaseName;..." providerName="System.Data.SqlClient" />
</connectionStrings>

in your DataContext of Entity-Framework you can pass the key in the web.config file 在实体框架的DataContext中,您可以在web.config文件中传递密钥

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext()
        : base("ConnectionStringName")
    {
    }

    // DbSet properties...
}

To read from configuration file and take a string you have to add a reference for System.Configuration assembly and try this: 要从配置文件读取并获取字符串,您必须添加System.Configuration程序集的引用,然后尝试以下操作:

using System.Configuration;

string connectionString = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM