"EntityConnection can only be constructed with a closed DbConnection" This is the problem I get when a try to construct an entityconnection providing an open connection. There is a transactionscope open and I don't want to open a new connection or the transaction would be promoted to a dtc transaction as my understanding is 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;
}
ObjectContext
instance? – Eranga