EntityConnection 只能用封闭的 DbConnection 来构造
“EntityConnection 只能用封闭的 DbConnection 来构造” 这是我在尝试构建提供开放连接的实体连接时遇到的问题。 有一个打开的事务范围,我不想打开一个新连接,否则该事务将被提升为 dtc 事务,因为我的理解是,如果我在多个实体连接上使用单个 SqlConnection,我不需要 DTC。
所以,我的代码大约是这样的。
提前致谢...
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;
}
"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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
雅库布是完全正确的。您既不能创建
DbContext
也不能创建EntityConnection
并将打开的DbConnection
传递给它们。根据 Diego B Vega 的帖子 此问题在 EF 6 发布之前不会得到修复 (这里你可以投票)
解决方法是在之前打开已经初始化的
EntityConnection
任何涉及它的操作。给定
ObjectContext
,您可以像这样打开EntityConnection
:在
DbContext
的情况下,EntityConnection
可从底层获得对象上下文
。代码可能是这样的:Jakub is completely right. You can create neither
DbContext
norEntityConnection
with openedDbConnection
passed to them.According to Diego B Vega’s post this issue will not be fixed until EF 6 release (here you can vote for it)
The workaround is to open already initialized
EntityConnection
before any operations involving it.Given
ObjectContext
you can openEntityConnection
like this:In case of
DbContext
theEntityConnection
is available from underlyingObjectContext
. The code might be like this:正如您已经发现的,您无法从打开的连接创建新的
EntityConnection
。您应该重构代码并传递
ObjectContext
,而不是一遍又一遍地传递SqlConnection
和创建EntityConnection
。ObjectContext
是 EF 中的根对象,您应该更喜欢它而不是SqlConnection
。As you've already found out, you cannot create a new
EntityConnection
from an opened connection.Instead of passing
SqlConnection
around and creatingEntityConnection
over and over again you should refactor your code and passObjectContext
instead.ObjectContext
is the root object in EF and you should favour it overSqlConnection
.