Entity Framework 的 DLL 无法连接到数据库

Dll with Entity Framework unable to connect to database

我有一个单独的 dll 来处理与数据库的连接,从而保存模型和 api 上下文。 app.config 'acts' 类似于 web.config 并保存连接详细信息但是我不断收到以下错误

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)

所以我从另一个更传统的mvc web.config文件中复制了连接字符串,但仍然出现错误。

奇怪的是,以下工作:

 update-database -verbose

我的 app.config 具有以下连接字符串:

<add name="DefaultConnection" 
     connectionString="data source=(LocalDb)\v11.0;initial catalog=APIEntities-01;persist security info=True; Integrated Security=SSPI;MultipleActiveResultSets=True" 
     providerName="System.Data.SqlClient" />

并且 dbContext 指向它

 public ApiContext() : base("DefaultConnection")
 {
 }

正如我所说,这在传统 MVC web.config 中有效,但使用引用 Entity Framework 之类的 dll 似乎无法找到数据库。有什么建议吗?

EF 6

我想是连接字符串,因为 "data source=(LocalDb)\v11.0;initial catalog=APIEntities-01" 是您 PC 的本地路径。您必须提供您需要使用的真实 SQL 服务器。看到这个:

Change default location of LocalDb

有时 sql 服务器打开失败。然后打开sql server configuration 然后select (sql server Services) 查看状态是stopped还是manual...右键点击然后make start...然后问题会出现得到解决。