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


  1. An SqlConnection is used on an existing website I have.
  2. The Entity Framework has been added.
  3. I've built the.edmx file and connected properly to the database.
  4. With the connectionstring that is produced automatically, I can connect using Entity Framework.

For the Entity Framework connection, I want to utilize the same SqlConnection object that I use across the website.
I don't want to alter the whole site to use the new Entity Framework connection string, nor do I want to use a second database connection for the one page that will utilize the Entity Framework.

I appreciate any assistance you may provide.

2/5/2009 11:50:04 PM

Accepted Answer

The solution is in the forum post:

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)

The portion of your EF connection string that begins with "res:/*/" specifies where your xml mapping files are located; in this example, they are embedded resources in the current assembly.

2/6/2009 7:00:40 AM

Popular Answer

Darren Peters

I appreciate your response.

I have been circling the System endlessly. Data.EntityClient.EntityConnection.

I have the MetadataWorkspace parameter there in front of me, but I can't seem to make it work.

Here is the closest illustration I could find (in the comment labelled "Answer"):

I appreciate any support.

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