Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityException未处理,基础提供程序无法打开_C#_Entity Framework - Fatal编程技术网

C# EntityException未处理,基础提供程序无法打开

C# EntityException未处理,基础提供程序无法打开,c#,entity-framework,C#,Entity Framework,我正在使用VisualStudio2012进行开发。我创建了一个类库EMPDAL,在这里我使用NorthWnd数据库的Employees表与Entity Framework连接,并在配置数据库时测试了连接。我写了下面的代码来获取员工的详细信息 public class EmplooyeeData { public static List<Employee> GetEmployees( int EmployeeId) { using (DbEntitie

我正在使用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();
        }
    }
}
但是,当客户端代码调用GetEmployees1并且调试器进入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=&quot;data source=localhost;initial catalog=NorthWnd;user id=sa;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>

您至少提到了应用程序类库和控制台应用程序的两层。N层应用程序要求在实体框架引用的应用程序所有层的web.config/app.config中声明EF数据库的连接字符串

确保在控制台应用程序app.config以及类库的app.config中列出了数据库的连接字符串

根据@Yuliam的上述评论,值得确保您的连接字符串中包含以下内容:

Integrated security=True;

当它设置为True时,如果指定用户和密码,.Net将尝试打开与当前用户事件的连接。如果要以特定用户身份打开连接字符串,请将集成安全设置为False

可能需要将配置中的连接字符串从DAL复制到控制台应用程序,什么是内部异常?我已在帖子中添加了我的Appconfig详细信息,并且我正在为我的客户端应用程序使用相同的Appconfig。能否显示内部异常?内部异常是基础提供程序在打开时失败。请尝试添加Integrated security=True;,MultipleActiveResultSets之后=True;我已经在我的帖子中添加了我的Appconfig详细信息,并且我正在为我的客户端应用程序使用相同的Appconfig。
Integrated security=True;