EntityConnection和打开的SqlConnection [英] EntityConnection and Opened SqlConnection

查看:404
本文介绍了EntityConnection和打开的SqlConnection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于使用CodeFirst方法的EntityFramework的问题。基于EntityConnection源代码和文档,我无法通过已经打开的SqlConnection创建它。它要求这是关闭的。
我们有一些不同的数据库数据层(nHibernate等),如果我们可以在它们之间重用同一个SqlConnection,那将是很好的。但是似乎EntityFramework不允许这样做。我可以将封闭的SqlConnection传递给DbContext,但是关闭每个DbContext的连接是不利的。



有没有办法重用打开的sql连接? p>

编辑



在EF转到OpenSource之后,我可以更新它。我修改的唯一一行是System.Data.Entity.Core.EntityClient.EntityConnection类的构造函数。

  if(connection .State!= ConnectionState.Closed)
{
// throw new ArgumentException(Strings.EntityClient_ConnectionMustBeClosed);
}

在我评论了throw异常之后,我可以重用现有的打开的SQL Connection,在DBContext使用它之后,它不会关闭它,它仍然可用于其他的东西。



我的示例代码:

  var connection = new SqlConnection(connectionString); 
connection.Open();

MetadataWorkspace workspace = GetWorkspace(connection); //我从相同的上下文加载它,并将其保存在内存中。

EntityConnection eConnection = new EntityConnection(工作空间,连接);

using(var context = new EFContext(connection,false))
{
var someData = context.SomeTable.ToList();
}

connection.Close();


解决方案

目前不支持将打开的连接传递给上下文,因为上下文内部始终使用 EntityConnection ,如果您检查文档,您将看到传递的存储连接必须处于关闭状态。



解决方法可以在使用其他数据访问技术之前创建上下文,并显式打开连接



  objectContext.Connection.Open(); 

现在你应该可以重用 SqlConnection 在上下文的范围内:

  var dbConnection =((EntityConnection)objectContext.Connection).StoreConnection; 

如果您使用DbContext API,我建议您阅读这篇文章有关如何解决 DbContext 的问题的其他详细信息。


I've a question about EntityFramework with CodeFirst approach. Based on EntityConnection source code and documentation I can't create it with already opened SqlConnection. It requires that is should be closed. We have some different DB data layers(nHibernate,etc), and it would be good, if we can reuse the same SqlConnection between them. But seems, that EntityFramework doesn't allow to do this. I can pass closed SqlConnection to DbContext, but it isn't good for me to close connection for each DbContext.

Is there a way to reuse opened sql connection?

EDIT

After EF goes to OpenSource I was able to update it. The only line I changed is in the constructor of System.Data.Entity.Core.EntityClient.EntityConnection class.

if (connection.State != ConnectionState.Closed)
{
//throw new ArgumentException(Strings.EntityClient_ConnectionMustBeClosed);
}

After I commented throw exception, I can able to reuse existing opened SQL Connection, and after DBContext used it, it won't close it, and it was still available to other stuff.

My sample code:

var connection = new SqlConnection(connectionString);
connection.Open();

MetadataWorkspace workspace = GetWorkspace(connection); // I loaded it from the same context, and save it in memory.

EntityConnection eConnection = new EntityConnection(workspace, connection);

using(var context = new EFContext(connection, false))
{
    var someData = context.SomeTable.ToList();
}

connection.Close();

解决方案

Passing opened connection to context is currently not supported because context internally always uses EntityConnection and if you check documentation you will see that passed store connection must be in closed state.

The workaround can be creating context prior to usage of other data access technologies and explicitly open connection

objectContext.Connection.Open();

Now you should be able to reuse SqlConnection inside the scope of the context:

var dbConnection = ((EntityConnection)objectContext.Connection).StoreConnection;

If you are using DbContext API I recommend you reading this article for additional details how to solve the problem with DbContext.

这篇关于EntityConnection和打开的SqlConnection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆