How can I utilize Entity Framework with a SQLConnection that already exists? entity-framework sqlconnection


  1. I have an existing website that uses an SqlConnection.
  2. I have added the Entity Framework.
  3. I have successfully connected to the database and created the .edmx file.
  4. I am able to connect through the Entity Framework with the connectionstring that is automatically generated.

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

2/5/2009 11:50:04 PM

Accepted Answer

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)

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

2/6/2009 7:00:40 AM

Popular Answer

Andrew Peters,

Thank you for your answer.

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

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

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

Thanks for any help.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow