![](/img/trans.png)
[英]EntityException: The underlying provider failed on Open + inner exceptions
[英]EntityException was unhandled the underlying provider failed to open
我正在使用VisualStudio2012进行开发。 我创建了一个类库EMPDAL
,在其中使用NorthWnd
数据库的Employees表与Entity Framework连接,并在配置数据库时测试了连接。 我写了下面的代码来获取员工的详细信息。
public class EmplooyeeData
{
public static List<Employee> GetEmployees( int EmployeeId)
{
using (DbEntities dbContext = new DbEntities())
{
return dbContext.Employees.Where(x => x.EmployeeID == EmployeeId).ToList();
}
}
}
我创建了一个控制台应用程序来使用此EMPDAL
因此我已对EMPDAL
进行了引用,并使用以下代码检索员工详细信息
static void Main(string[] args)
{
List<EmpDAL.Employee> emp = new List<EmpDAL.Employee>();
emp = EmpDAL.EmplooyeeData.GetEmployees(1);
}
但是,当客户端代码调用GetEmployees(1)
且调试器进入EMPDAL.EmpDataGetEmployess
方法时,则在return语句中它将引发异常![异常如下所示]
基础提供程序在打开时失败。
AppConfig我在类库和客户端应用程序中使用了相同的控件。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DbEntities" connectionString="metadata=res://*/EmployeeModel.csdl|res://*/EmployeeModel.ssdl|res://*/EmployeeModel.msl;provider=System.Data.SqlClient;provider connection string="data source=localhost;initial catalog=NorthWnd;user id=sa;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
您在应用程序中至少提到了两层(类库和控制台应用程序)。 N层应用程序要求您具有在Entity Framework所引用的应用程序所有层的web.config / app.config中声明的EF数据库的连接字符串。
确保在控制台应用程序app.config和类库的app.config中列出了数据库的连接字符串。
根据@Yuliam的上述评论,值得确保连接字符串中包含以下内容:
Integrated security=True;
这是做什么的,当它设置为True时,.Net会尝试使用当前用户事件打开连接,如果您指定了用户和密码。 如果要以特定用户身份打开连接字符串,请将Integrated Security设置为False。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.