简体   繁体   English

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

[英]EntityException was unhandled the underlying provider failed to open

I am using VisualStudio2012 for the development. 我正在使用VisualStudio2012进行开发。 I have created a class library EMPDAL where I am using Employees table of NorthWnd database to connect with Entity Framework and I tested the connection while configuring the database. 我创建了一个类库EMPDAL ,在其中使用NorthWnd数据库的Employees表与Entity Framework连接,并在配置数据库时测试了连接。 I have written below code to get employees details. 我写了下面的代码来获取员工的详细信息。

public class EmplooyeeData
{
    public static List<Employee> GetEmployees( int EmployeeId)
    {
        using (DbEntities dbContext = new DbEntities())
        {
            return dbContext.Employees.Where(x => x.EmployeeID == EmployeeId).ToList();
        }
    }
}

I created a console application to use this EMPDAL so I had given the reference to my EMPDAL and using below code to retrieve employee details 我创建了一个控制台应用程序来使用此EMPDAL因此我已对EMPDAL进行了引用,并使用以下代码检索员工详细信息

static void Main(string[] args)
{
   List<EmpDAL.Employee> emp = new List<EmpDAL.Employee>();
   emp = EmpDAL.EmplooyeeData.GetEmployees(1);
}

But when client code calls GetEmployees(1) and debugger goes in to the EMPDAL.EmpDataGetEmployess method then in return statement it throws the exception ![Exception is shown below] 但是,当客户端代码调用GetEmployees(1)且调试器进入EMPDAL.EmpDataGetEmployess方法时,则在return语句中它将引发异常![异常如下所示]

The underlying provider failed on Open. 基础提供程序在打开时失败。

AppConfig I have used same in my class library and client application. 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>

You've made mention of at least two tiers to your application (class library and a console application). 您在应用程序中至少提到了两层(类库和控制台应用程序)。 N-tier applications require you to have the connection string for your EF database to be declared in the web.config / app.config of all tiers of your application that Entity Framework is referenced in. N层应用程序要求您具有在Entity Framework所引用的应用程序所有层的web.config / app.config中声明的EF数据库的连接字符串。

Ensure you have the connection string for your database listed in your console applications app.config as well as the class library's app.config. 确保在控制台应用程序app.config和类库的app.config中列出了数据库的连接字符串。

As per the above comments from @Yuliam, it is worth ensuring you have the following in your connection strings: 根据@Yuliam的上述评论,值得确保连接字符串中包含以下内容:

Integrated security=True;

What this does is When it is set to True, .Net tries to open the connection with the current user event if you specify a user and password. 这是做什么的,当它设置为True时,.Net会尝试使用当前用户事件打开连接,如果您指定了用户和密码。 Set the Integrated Security to False if you want to open the connection string as a specific user. 如果要以特定用户身份打开连接字符串,请将Integrated Security设置为False。

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

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