简体   繁体   English

错误:基础提供程序在打开时失败。 怎么解决呢?

[英]Error: The underlying provider failed on Open. How to resolve it?

I have function that with code similar to this: 我的功能类似于以下代码:

using(var db = new MyDbContext())
{
    //a whole bunch of code, db is working.

    //then I try opening another DbContext like so
    using(var dba = new AnotherDbContext())
    {
        //about 2 lines of code just to get something from a database
    }
}

And then I get 2 error messages when I get to the second DbContext: 然后,当我到达第二个DbContext时,我收到2条错误消息:

"The underlying provider failed on Open." “底层提供程序在Open上失败。” & "MSDTC on server 'myserver' is unavailable." &“服务器'myserver'上的MSDTC不可用。”

Does anybody know the reason why this is happening? 有人知道发生这种情况的原因吗? Can I open 2 DbContexts at once? 我可以一次打开2个DbContext吗?

In the first scenario, you are nesting AnotherDbContext . 在第一种情况下,您将嵌套AnotherDbContext A connection to the database is opened for each on of them. 将为每个数据库打开与数据库的连接。 When you call your service method within the using block, a new connection is opened within the MyDbContextwhile there is another one already open. 当您在using块中调用服务方法时,将在MyDbContext中打开一个新连接,而另外一个已经打开。 This causes your transaction to be promoted to a distributed transaction, and partially committed data (the result of the dba.SaveChanges call in the service) not being available from your outer connection.Also note that distributed transactions are far slower and thus, this has the side effect of degrading performance. 这会导致您的事务升级为分布式事务,并且外部连接无法使用部分提交的数据(该服务中dba.SaveChanges调用的结果)。 还要注意,分布式事务的速度要慢得多,因此降低性能的副作用。

private void btnTwoConnectionsNested_Click(object sender, EventArgs e)
{
    string connectionString = @"Data Source=" + tbServer.Text
        + @";Initial Catalog=master;Integrated Security=True; timeout=0";

    using (TransactionScope transactionScope = new TransactionScope())
    {
        SqlConnection connectionOne = new SqlConnection(connectionString);
        SqlConnection connectionTwo = new SqlConnection(connectionString);

        try
        {
            //2 connections, nested
            connectionOne.Open();
            connectionTwo.Open(); // escalates to DTC on 05 and 08
            connectionTwo.Close();
            connectionOne.Close();

            MessageBox.Show("Success");
        }
        catch (Exception ex)
        {
            MessageBox.Show("ERROR: " + ex.Message);
        }
        finally
        {
            connectionOne.Dispose();
            connectionTwo.Dispose();
        }
    }
}

Within one Transaction Scope, it will open two connections, nested. 在一个事务范围内,它将打开两个嵌套的连接。 The result: 结果:

Against SQL Server 2005 Instance: MSDTC on server 'SERVERNAME' is unavailable. 针对SQL Server 2005实例:服务器“ SERVERNAME”上的MSDTC不可用。

Against SQL Server 2008 Instance: 针对SQL Server 2008实例:
MSDTC on server 'SERVERNAME' is unavailable. 服务器“ SERVERNAME”上的MSDTC不可用。

SQL Server does, and should, escalate to DTC for a nested connection in both versions. 对于两个版本的嵌套连接,SQL Server都(并且应该)升级为DTC。

Nested connections will escalate to DTC in SQL Server 2005 and 2008. Opening one connection at a time will escalate to DTC in 2005, but WILL NOT in 2008. 嵌套连接将在SQL Server 2005和2008中升级为DTC。一次打开一个连接将在2005年升级为DTC,但在2008年不会升级。

暂无
暂无

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

相关问题 基础提供程序在打开时失败。 MVC - The underlying provider failed on Open. MVC 错误:基础提供程序在打开时失败。 用户的system.data.sqlclient.sqlexception登录失败 - Error: the underlying provider failed on open. system.data.sqlclient.sqlexception login failed for user System.Data.Entity.Core.EntityException:“基础提供程序在打开时失败。” 错误? - System.Data.Entity.Core.EntityException: 'The underlying provider failed on Open.' Error? 在Parallel.ForEach中:基础提供程序在打开时失败。 使用EF5 - in Parallel.ForEach : The underlying provider failed on Open. with EF5 实体框架给出异常:“底层提供程序在打开时失败。” - Entity Framework giving exception : “The underlying provider failed on Open.” 带有 Unity 的实体框架“底层提供程序在 Open 上失败”。 - Entity framework with Unity “The underlying provider failed on Open.” 内部异常 #1:MSG:底层提供程序在打开时失败。 在服务器上 - INNER EXCEPTION #1: MSG: The underlying provider failed on Open. On Server 使用 TopShelf 的 Windows 服务:底层提供程序在打开时失败。 用户登录失败 - Windows service using TopShelf: The underlying provider failed on Open. User logon failure MVC连接错误:基础提供程序在打开时失败 - MVC Connection Error : The underlying provider failed on Open MSSQL 错误“底层提供程序在打开时失败” - MSSQL Error 'The underlying provider failed on Open'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM