如何将ADO.net Entity Framework与现有的SqlConnection一起使用?

时间:2021-07-22 06:53:38
  1. I have an existing asp.net website that uses an SqlConnection.
  2. 我有一个使用SqlConnection的现有asp.net网站。

  3. I have added the ADO.net Entity Framework.
  4. 我添加了ADO.net实体框架。

  5. I have successfully connected to the database and created the .edmx file.
  6. 我已成功连接到数据库并创建了.edmx文件。

  7. I am able to connect through the Entity Framework with the connectionstring that is automatically generated.
  8. 我能够通过Entity Framework连接自动生成的connectionstring。

I want to use the existing SqlConnection object that I use throughout the site for the Entity Framework connection.
I do not want to have to use a second database connection for the one page that is going to use the ADO.net Entity Framework and I don’t want to change the entire site to use the new Entity Framework connection string.

我想使用我在整个站点中用于Entity Framework连接的现有SqlConnection对象。我不想为将要使用ADO.net实体框架的一个页面使用第二个数据库连接,并且我不想更改整个站点以使用新的Entity Framework连接字符串。

Thanks for any help you can provide.

感谢您的任何帮助,您可以提供。

3 个解决方案

#1


That forum post has the answer:

该论坛帖子有答案:

MetadataWorkspace workspace = new MetadataWorkspace(
  new string[] { "res://*/" }, 
  new Assembly[] { Assembly.GetExecutingAssembly() });

using (SqlConnection sqlConnection = new SqlConnection(connectionString))
using (EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection))
using (NorthwindEntities context = new NorthwindEntities(entityConnection))
{
  foreach (var product in context.Products)
  {
    Console.WriteLine(product.ProductName);
  }
}

"res://*/" is the part of your EF connection string that describes the location of your xml mapping files - in this case embedded resources in the current assembly.

“res:// * /”是EF连接字符串的一部分,用于描述xml映射文件的位置 - 在本例中是当前程序集中的嵌入资源。

#2


You can do this by using the constructor of your generated ObjectContext that accepts an EntityConnection. When you create the EntityConnection you pass in your SqlConnection.

您可以使用生成的接受EntityConnection的ObjectContext的构造函数来完成此操作。创建EntityConnection时,您将传入SqlConnection。

#3


Andrew Peters,

Thank you for your answer.

谢谢您的回答。

I have been going around and around with the System.Data.EntityClient.EntityConnection.

我一直在使用System.Data.EntityClient.EntityConnection。

It’s right there at my finger tips but I cannot seem to get the MetadataWorkspace parameter to work.

它就在我的指尖,但我似乎无法让MetadataWorkspace参数工作。

This is the closest example I have found (the post marked Answer):

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/dd7b1c41-e428-4e29-ab83-448d3f529ba4/

Thanks for any help.

谢谢你的帮助。

#1


That forum post has the answer:

该论坛帖子有答案:

MetadataWorkspace workspace = new MetadataWorkspace(
  new string[] { "res://*/" }, 
  new Assembly[] { Assembly.GetExecutingAssembly() });

using (SqlConnection sqlConnection = new SqlConnection(connectionString))
using (EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection))
using (NorthwindEntities context = new NorthwindEntities(entityConnection))
{
  foreach (var product in context.Products)
  {
    Console.WriteLine(product.ProductName);
  }
}

"res://*/" is the part of your EF connection string that describes the location of your xml mapping files - in this case embedded resources in the current assembly.

“res:// * /”是EF连接字符串的一部分,用于描述xml映射文件的位置 - 在本例中是当前程序集中的嵌入资源。

#2


You can do this by using the constructor of your generated ObjectContext that accepts an EntityConnection. When you create the EntityConnection you pass in your SqlConnection.

您可以使用生成的接受EntityConnection的ObjectContext的构造函数来完成此操作。创建EntityConnection时,您将传入SqlConnection。

#3


Andrew Peters,

Thank you for your answer.

谢谢您的回答。

I have been going around and around with the System.Data.EntityClient.EntityConnection.

我一直在使用System.Data.EntityClient.EntityConnection。

It’s right there at my finger tips but I cannot seem to get the MetadataWorkspace parameter to work.

它就在我的指尖,但我似乎无法让MetadataWorkspace参数工作。

This is the closest example I have found (the post marked Answer):

http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/dd7b1c41-e428-4e29-ab83-448d3f529ba4/

Thanks for any help.

谢谢你的帮助。