为什么连接字符串必须在客户端项目中而不是DAL中



嗯,我有两个项目:

  • Blog.Core(我的实体的类库项目以及数据访问代码)
  • 博客(ASP.NET MVC项目)

我在Blog.Core中安装了EF。然后在博客添加了对Blog.Core的引用。我已经在Blog.Core中指定了我的连接字符串,因为这是我的DAL。现在,当我运行该项目时,我得到以下错误:

An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code
Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

我删除了Blog.Core中的连接字符串,并将其放入Blog项目中,它就工作了。为什么?这不是因为客户端有数据库连接字符串而破坏了体系结构吗?我的意思是,只有DAL应该负责连接字符串和数据库操作,这不是正确的吗。为什么我的第一个(根据我的说法,更合乎逻辑的)解决方案不起作用?

关于这个有很多争论,你可以参考这个线程的C#DLL配置文件

相关内容

最新更新