如何使用ADO.net实体框架与现有的SqlConnection? [英] How to use ADO.net Entity Framework with an existing SqlConnection?

查看:77
本文介绍了如何使用ADO.net实体框架与现有的SqlConnection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 在我有一个使用的SqlConnection现有的asp.net网站。
  2. 我已经加入了ADO.net实体框架。
  3. 在我已经成功地连接到数据库并创建了.edmx文件。
  4. 在我能够通过实体框架与自动生成的ConnectionString的连接。

我想用我整个网站的实体框架连接使用现有SqlConnection对象。
我不希望有使用的一页,是要使用ADO.net实体框架第二个数据库连接,我不想改变整个站点使用了新的实体框架的连接字符串。

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.

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

Thanks for any help you can provide.

推荐答案

这是论坛上发帖给出了答案:

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);
  }
}

水库:// * /。是你的EF连接字符串的描述你的XML映射文件的位置的部分 - 在这种情况下,在目前组件嵌入的资源

"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.

这篇关于如何使用ADO.net实体框架与现有的SqlConnection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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