简体   繁体   English

实体框架底层提供程序在打开时失败

[英]Entity Framework The underlying provider failed on Open

Below is my connection string:下面是我的连接字符串:

connectionString="metadata=res://*/EDMX.Test.csdl|res://*/EDMX.Test.ssdl|res://*/EDMX.Test.msl;provider=System.Data.SqlClient;provider connection string="Data Source=home_computer;Initial Catalog=db_Test;Persist Security Info=True;User ID=testUser;Password=$1234;MultipleActiveResultSets=True"" connectionString="metadata=res://*/EDMX.Test.csdl|res://*/EDMX.Test.ssdl|res://*/EDMX.Test.msl;provider=System.Data.SqlClient;provider连接字符串="数据源=home_computer;初始目录=db_Test;持久安全信息=True;用户ID=testUser;密码=$1234;MultipleActiveResultSets=True""

Here is the code where the program stuck:这是程序卡住的代码:

EDMX.TestingEntity context = new EDMX.TestingEntity();

var query = from t in context.User
            where t.UserName == _userName
            select t;

After running the above code, I check the variable query and found an exception运行上述代码后,查看变量查询,发现异常

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

I've checked:我检查过:

  1. Connection between server and computer is normal服务器与电脑连接正常
  2. I can login to the database with username testuser and with password $1234我可以使用用户名 testuser 和密码 $1234 登录数据库
  3. I have checked the security settings in database (SQL Server) that permission has been granted to testUser我已经检查了数据库(SQL Server)中的安全设置,该权限已被授予 testUser

Why does this exception happen?为什么会发生这个异常? I'm using .net 4.5我正在使用 .net 4.5


Added:添加:

I tried again, look at the inner exception and it was: A network-related or instance-specific error occurred while establishing a connection to SQL Server.我再次尝试,查看内部异常,它是:建立与 SQL 服务器的连接时发生网络相关或特定于实例的错误。 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.验证实例名称是否正确,并且 SQL 服务器配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供者,错误:40 - 无法打开与 SQL 服务器的连接)

I know it might be a network problem but I've turned off the firewall of server and also my computer and tried again but still not success..我知道这可能是网络问题,但我已经关闭了服务器的防火墙和我的电脑,然后再次尝试但仍然没有成功..


Just now Copied the connectionstring to a program to test this connection and it was functioning well..刚才将连接字符串复制到程序以测试此连接并且它运行良好..


I just rollback all changes and test again and it worked我只是回滚所有更改并再次测试,它工作

Seems like a connection issue.好像是连接问题。 You can use the Data link properties to find if the connection is fine.您可以使用数据链接属性来确定连接是否正常。 Do the following:请执行下列操作:

  1. Create a blank notepad and rename it to "X.UDL"创建一个空白记事本并将其重命名为“X.UDL”
  2. Double click to open it双击打开
  3. Under connections tab choose the server name/enter the name use the correct credentials and DB在连接选项卡下选择服务器名称/输入名称使用正确的凭据和数据库
  4. Click OK to save it.单击确定以保存它。

Now open the file in Notepad and compare the connection string properties.现在在记事本中打开文件并比较连接字符串属性。

  1. Search "Component Services" in Programs and Files在程序和文件中搜索“组件服务”
  2. Go to Services前往服务
  3. Find "Distributed Transaction Coordinator" Service查找“分布式事务协调器”服务
  4. Right click and Restart the Service右键单击并重新启动服务

You've just done a restart of the service and the code should run without errors您刚刚重新启动了服务,代码应该可以正常运行

We had connection string in web.config with Data Source=localhost , and there was this error (MSSQL was on the same machine).我们在web.configData Source=localhost连接字符串,并且出现了这个错误(MSSQL 在同一台机器上)。 Changing it to actual `DOMAIN\\MACHINE' helped, somewhy.不知何故,将其更改为实际的“DOMAIN\\MACHINE”有所帮助。

Possible solution is described in this Code Project tip : 此代码项目提示中描述了可能的解决方案:

As folks mentioned IIS user network service user credentials while trying to log in sql server.正如人们在尝试登录 sql server 时提到的 IIS 用户网络服务用户凭据。 So just change the Application pool settings in your IIS:因此,只需更改 IIS 中的应用程序池设置:

  1. Open Internet Information Service Manager打开 Internet 信息服务管理器
  2. Click on Application Pools in left navigation tree.单击左侧导航树中的应用程序池。
  3. Select your version Pool.选择您的版本池。 In my case, I am using ASP .Net v4.0.就我而言,我使用的是 ASP .Net v4.0。 If you dont have this version, select DefaultAppPool.如果您没有此版本,请选择 DefaultAppPool。
  4. Right click on step 3, and select advanced settings.右键单击第 3 步,然后选择高级设置。
  5. Select Identity in properties window and click the button to change the value.在属性窗口中选择 Identity 并单击按钮更改值。
  6. Select Local System in Built-in accounts combo box and click ok.在内置帐户组合框中选择本地系统,然后单击确定。 That's it.而已。 Now run your application.现在运行您的应用程序。 Everything works well.一切正常。

Always check for Inner Exception if any.如果有,请始终检查内部异常。 In my case Inner Exception turned out to be really helpful in figuring out the issue.就我而言,内部异常对解决问题非常有帮助。

My site was working fine in Dev Environment.我的网站在开发环境中运行良好。 But after i deployed to production, it started giving out this exception, but the Inner Exception was saying that Login failed for the particular user.但是在我部署到生产环境后,它开始发出这个异常,但内部异常表示特定用户的登录失败。
So i figured out it was something to do with the connection itself.所以我发现这与连接本身有关。 Hence tried logging in using SSMS and even that failed.因此尝试使用 SSMS 登录,甚至失败了。

Eventually figured out that exception showed up for the simple reason that the SQL server had only Windows Authentication enabled and SQL Authentication was failing which was what i was using for Authentication.最终发现出现异常的原因很简单,即 SQL 服务器仅启用了 Windows 身份验证,而 SQL 身份验证失败,这正是我用于身份验证的原因。

In short, changing Authentication to Mixed(SQL and Windows), fixed the issue for me.简而言之,将身份验证更改为混合(SQL 和 Windows),为我解决了这个问题。 :) :)

Please check the following things first.请先检查以下事项。

While generating the Edmx you would have given a name to you connection string.在生成 Edmx 时,您将为连接字符串命名。 that gets into the app config of the project with the Entity.与实体一起进入项目的应用程序配置。

Have you copied the same connection string to your main Config file.您是否将相同的连接字符串复制到主配置文件中。 Also the Name should be same as which you have given while generating the EDMX file.此外,名称应与您在生成 EDMX 文件时提供的名称相同。

My client reported this error.我的客户报告了这个错误。 I found that he was messing with *.ldf files.我发现他在搞乱 *.ldf 文件。 He copied *ldf file on one database and renamed it to match a second database (which I asked him to place in a folder).他复制了一个数据库上的 *ldf 文件并将其重命名以匹配第二个数据库(我让他将其放在一个文件夹中)。

I replicated the same scenario, and got this same error in my development system.我复制了相同的场景,并在我的开发系统中遇到了同样的错误。 Error got fixed after deleting the *ldf file(s).删除 *ldf 文件后错误得到修复。

I saw this error when a colleague was trying to connect to a database that was protected behind a VPN.当一位同事尝试连接到受 VPN 保护的数据库时,我看到了此错误。 The user had unknownling switched to a wireless network that did not have VPN access.用户不知不觉地切换到了没有 VPN 访问权限的无线网络。 One way to test this scenario is to see if you can establish a connection in another means, such as SSMS, and see if that fails as well.测试此场景的一种方法是查看您是否可以通过其他方式(例如 SSMS)建立连接,并查看是否也失败。

For me when that usually starts happening, I have to remote desktop into the service and at the minimum restart IIS.对我来说,当这通常开始发生时,我必须远程桌面进入服务并至少重新启动 IIS。 It usually starts popping up right after I deploy code.它通常在我部署代码后立即开始弹出。 On a few rare occasions I have had to restart the SQL services and IIS.在少数情况下,我不得不重新启动 SQL 服务和 IIS。 I wrote a batch script to take a param (1 or 2) and have it setup to either do a restart of IIS ( ie 1), or go full nuclear (ie 2).我编写了一个批处理脚本来获取参数(1 或 2),并将其设置为重新启动 IIS(即 1)或全核(即 2)。

I get this exception often while running on my development machine, especially after I make a code change, rebuild the code, then execute an associated web page(s).在我的开发机器上运行时,我经常遇到这个异常,尤其是在我更改代码、重建代码,然后执行关联的网页之后。 However, the problem goes away for me if I bump up the CommandTimeout parameter to 120 seconds or more (eg, set context.Database.CommandTimeout = 120 before the LINQ statement).但是,如果我将 CommandTimeout 参数提高到 120 秒或更长时间(例如,在 LINQ 语句之前设置 context.Database.CommandTimeout = 120),问题就会消失。 While this was originally asked 3 years ago, it may help someone looking for an answer.虽然这最初是 3 年前提出的,但它可能有助于寻找答案的人。 My theory is VisualStudio takes time to convert the built binary libraries to machine code, and times out when attempting to connect to SQL Server following that just-in-time compile.我的理论是 VisualStudio 需要时间将构建的二进制库转换为机器代码,并且在即时编译之后尝试连接到 SQL Server 时会超时。

In my case, I resolved the error by adding connection password in the connection string.就我而言,我通过在连接字符串中添加连接密码解决了错误。

While setting up the EF model, I had selected the option to exclude sensitive data from connection string.在设置 EF 模型时,我选择了从连接字符串中排除敏感数据的选项。 So, the password was not included initially.因此,最初不包含密码。

I got this problem while continuing execution of a unit test that calls a method that is using parallel processing.I know there are parts of EF that are not thread-safe, so I am wondering if it is a conflict where the connection is being open and closed out of sync with the operations.我在继续执行调用使用并行处理的方法的单元测试时遇到了这个问题。我知道 EF 的某些部分不是线程安全的,所以我想知道连接打开是否存在冲突并关闭与操作不同步。

My stack trace showed this:我的堆栈跟踪显示了这一点:

     at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
   at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at System.Threading.Tasks.Task.Wait()
   at System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Func`4 bodyWithLocal, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IList`1 list, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable`1 source, ParallelOptions parallelOptions, Action`1 body, Action`2 bodyWithState, Action`3 bodyWithStateAndIndex, Func`4 bodyWithStateAndLocal, Func`5 bodyWithEverything, Func`1 localInit, Action`1 localFinally)
   at System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable`1 source, Action`1 body)

So that's the clue I followed.所以这就是我遵循的线索。 When I went back to a single-thread foreach instead of Parallel.ForEach the issue went away.当我回到单线程 foreach 而不是 Parallel.ForEach 时,问题就消失了。

Joey乔伊

我遇到了这个错误,它是由 App.config 中的连接字符串中的拼写错误引起的。

I was facing the same error today, what I was doing wrong was that I was not adding Password tag in the connection string.我今天面临同样的错误,我做错的是我没有在连接字符串中添加密码标签。 As soon as I added the Password tag with correct password the error went away.一旦我用正确的密码添加了密码标签,错误就消失了。 Hope it helps someone.希望它可以帮助某人。

I faced the same issue.我遇到了同样的问题。 Though in my case I was trying to connect my desktop application to a remote db.虽然在我的情况下,我试图将我的桌面应用程序连接到远程数据库。 So for me, all the above didn't work.所以对我来说,以上所有方法都不起作用。 I solve this problem by just adding the port (as 128.02.39.29:3315 ) and it magically works!我通过添加端口(如128.02.39.29:3315 )解决了这个问题,它神奇地工作! The reason why I didn't bother to add the port in the first place is because I used same approach (without the port) in another desktop app and it worked.我之所以没有首先添加端口是因为我在另一个桌面应用程序中使用了相同的方法(没有端口)并且它有效。 So I hope this might help someone as well.所以我希望这也可以帮助某人。

This solution is for someone facing this issue while deploying a windows application ,此解决方案适用于在部署Windows 应用程序时遇到此问题的人,

I know this is late, but this maybe useful for someone in future, In my scenario, purely this is a connection string issue I developed a windows application using entity framework (DB First approach) and I published it, the code was worked fine on my machine, but it's not worked on the client machine我知道这已经晚了,但这可能对将来的某个人有用,在我的场景中,这纯粹是一个连接字符串问题,我使用实体框架(DB First 方法)开发了一个 Windows 应用程序并发布了它,代码运行良好我的机器,但它在客户端机器上不起作用

Reason for this issue:-出现此问题的原因:-

I updated the client machine connection string in App.config file, but this is wrong, if that is a windows application, then it will not read the connection string from App.config (For deployed machines), it will read from .exe.config file,我更新了 App.config 文件中的客户端机器连接字符串,但这是错误的,如果那是 Windows 应用程序,那么它不会从 App.config(对于已部署的机器)读取连接字符串,而是从 .exe 读取。配置文件,

So after deployment, we need to change the connection string in "AppicationName".exe.config file所以部署后,我们需要更改“AppicationName”.exe.config文件中的连接字符串

this error was solved my sql server password is expired when i create a new password then my app is login perfect此错误已解决我的 sql 服务器密码在我创建新密码时已过期然后我的应用程序登录完美

Try this- Open the command prompt as administrator and type this netsh Winsock reset试试这个 - 以管理员身份打开命令提示符并输入这个netsh Winsock reset

Restart your system and try again.重新启动系统并重试。

If you are using a local .mdf file, probably a sync software such Dropbox attempted to sync two log files (.ldf) in two different computers you can delete the log files from the bin Directory and make sure the .mdf properties->Copy to Output Directory ->Copy if newer that will copy the selected DB file and it's log to the bin Directory.如果您使用的是本地 .mdf 文件,可能是 Dropbox 等同步软件尝试同步两台不同计算机中的两个日志文件 (.ldf),您可以从 bin 目录中删除日志文件并确保 .mdf 属性->复制到输出目录 -> 如果较新,则复制将复制选定的数据库文件及其日志到 bin 目录。 !Alert- if your DB file has only changed in the bin Directory all the changes ill be discarded! !Alert- 如果您的数据库文件仅在 bin 目录中更改,则所有更改都将被丢弃!

打开 SQL Server 配置管理器,然后单击 sql server services 列表中将显示一个列表,右键单击 sql server,然后单击开始

ERROR : An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Additional information: The underlying provider failed on Open.错误:EntityFramework.SqlServer.dll 中出现“System.Data.Entity.Core.EntityException”类型的异常,但未在用户代码中处理附加信息:基础提供程序在打开时失败。

SOLUTION:解决方案:

  • Add in Model:添加模型:

     [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key]
  • Namespace:命名空间:

     using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema;
  • Example:例子:

     namespace MvcApplication1.Models { [Table("tblEmployee")] public class Employee { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Key] public int EmplyeeID { get; set; } public string Name { get; set; } public string Gender { get; set; } public string City { get; set; } } }

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

相关问题 基础提供程序无法打开-WPF和实体框架 - The underlying provider failed to open - WPF and Entity FrameWork 实体框架异常“底层提供程序打开失败” - Entity Framework Exception “The underlying provider failed on Open” 基础提供程序在实体框架中打开时失败 - The underlying provider failed on Open in entity framework connection NServiceBus和实体框架导致异常-基础提供程序在打开时失败 - NServiceBus and Entity Framework causes exception - The underlying provider failed on Open 在实体框架上下文中使用单例模式-基础提供程序在打开时失败 - Using singleton pattern with Entity Framework context - The underlying provider failed on open 实体框架和SQL Server CE-基础提供程序在打开时失败 - Entity Framework & SQL Server CE - Underlying Provider Failed On Open SQLite&Entity Framework 6“基础数据提供程序无法打开” - SQLite & Entity Framework 6 “The underlying data provider failed to open” 实体框架:基础提供程序在打开时失败-通过WCF服务 - Entity Framework: The underlying provider failed on Open - through WCF service 实体框架给出异常:“底层提供程序在打开时失败。” - Entity Framework giving exception : “The underlying provider failed on Open.” WCF和实体框架和SQL Server - “底层提供程序在Open上失败” - WCF & Entity Framework & SQL Server - “The underlying provider failed on Open”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM