none
EntityConnection can only be constructed with a closed DbConnection

    Question

  • "EntityConnection can only be constructed with a closed DbConnection"
    This is what i get when a try to construct an entityconnection providing an open connection ..
    This error poses is a big problem for my design .. there is a transactionscope open on the call stack AND i don't want to open a new connection or the transaction would be promoted to a dtc transaction .. is there any solution / workaround ?
    .. in other words how can i manage a transaction between linq to entity and another interaction with the same db (a plain ado.net command or another linq to entities ObjectContext) WITHOUT DTC ?

    I've a similar archictecture for LINQtoSQL and there i can create a DataContext providing an open connection without problem.

    thank in advance for the help
    best regards
    Enrico Sabbadin
    Monday, December 15, 2008 11:01 AM

Answers

  •  Enrico, you are correct in your analysis that EntityConnection requries an open DbConnection. I could not find a good reason on why this restriction is in place. I will follow-up on this issue with the team. However, in the interim you should be able create the EntityConneciton with the closed connection and then open it after the EntityConnection is instantiated to do non-EF operations and then use EF. This will allow you to use the same conneciton for both.
    For Example:
    DbConnection storeConnection = CreateConnectionToDb()  
    using (EntityConnction orignalConnection = new EntityConnection(str))  
    {  
        storeConnection.Open();  
        //Do Store specific actions here  
     
     
        //Do EF specific actions here  
    }  
     
     


    Hope this helps,
    Sushil.
    Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights
    Friday, March 27, 2009 10:46 PM

All replies

  •  Enrico, you are correct in your analysis that EntityConnection requries an open DbConnection. I could not find a good reason on why this restriction is in place. I will follow-up on this issue with the team. However, in the interim you should be able create the EntityConneciton with the closed connection and then open it after the EntityConnection is instantiated to do non-EF operations and then use EF. This will allow you to use the same conneciton for both.
    For Example:
    DbConnection storeConnection = CreateConnectionToDb()  
    using (EntityConnction orignalConnection = new EntityConnection(str))  
    {  
        storeConnection.Open();  
        //Do Store specific actions here  
     
     
        //Do EF specific actions here  
    }  
     
     


    Hope this helps,
    Sushil.
    Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights
    Friday, March 27, 2009 10:46 PM
  • I'm getting the same error.

    I was not able to use this solution because, I have multiple Entity Connections across a single transaction scope. I came to an understanding that if I use a single SqlConnection over the multiple entityConnections, I don't need DTC. So, my code is approximately like this.

     

    Thanks in advance.

    using (TransactionScope transactionScope = new TransactionScope())
    {
    
    	using (SqlConnection dwConn = GetDWConnection(user)) 
    	{
    		dwConn.Open();
    		// I need to do some SQlConnection specific actions first
    		
    		//EntityConnection specific actions next
    		Func1(dwConn);
    		Func2(dwConn); //similar to Func1()
    		Func3(dwConn); //Similar to Func1()
    	}
    }
    	Func1(SqlConnection dwConn)
    	{
    		using (EntityConnection conn = GetSQLEntityConnection(sqlConnection))
    		{
    			ObjectContext objectContext = (ObjectContext)Activator.CreateInstance(objectContextType, new object[] { conn });
    			//few actions
    		}
    	}
    	private EntityConnection GetSQLEntityConnection(SqlConnection sqlConnection)
    		{
    		//few steps
    			EntityConnection entityConnection = new EntityConnection(entityConnectionStringBuilder.ToString());
    			
    			EntityConnection sqlEntityConnection = new EntityConnection(entityConnection.GetMetadataWorkspace(),sqlConnection);
    			return sqlEntityConnection;
    		}
    		
    
    


     


    Sailoosha
    Thursday, August 25, 2011 4:02 PM
  • Hi Enrico,

    I found than working with underlying ObjectContext class you can control the DbConnection and DbTransaction classes and resue same physical Sql Server Transaction accross multiple contexts, without kicking in DTC.

     

    var context1 = new DbContext("<your connection string>");
    
    DbTransaction tx = null;
    
    DbConnection dbConnection = ((IObjectContextAdapter)context1).ObjectContext.Connection;
    
    try
    {
      dbConnection.Open();
      tx = dbConnection.BeginTransaction();
    
      // do whatever on context1
    
      var context2 = new DbContext(dbConnection, false);
    
      // do whatever on context2
    }
    finally
    {
      tx.Rollback();
      dbConnection.Dispose();
    }
    


    Hope this hepls.

     

    Omid Ehsani.

     

    • Proposed as answer by omid1 Tuesday, August 30, 2011 11:25 AM
    Tuesday, August 30, 2011 11:19 AM
  • Has your follow-up on this issue with the team had any response?  I am working in an environment where I get a connection that has other actions on it and may already be open.  I have used reflection to get around the problem by first creating a closed connection then replacing it with my open connection but that is not a production worth solution.  The problem is increased because the latest release of 4.1 does not allow for custom providers which is another solution I am pursuing.
    Thursday, October 27, 2011 8:06 PM