「EntityConnection只能使用封閉的DbConnection構建」 這是我嘗試構建提供開放式連接的實體連接時遇到的問題。 有一個事務處理器打開,我不想打開一個新的連接,或者事務將被提升爲一個dtc事務處理,因爲我的理解是,如果我通過多個entityConnections使用單個SqlConnection,我不需要DTC。EntityConnection只能使用封閉的DbConnection構建
所以,我的代碼大致是這樣的。
在此先感謝...
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'實例? – Eranga