How can I utilize ADO.net Entity Framework with a SQLConnection that already exists?

ado.net entity-framework sqlconnection

Question

  1. An SqlConnection is used on an existing asp.net website I have.
  2. The ADO.net 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 ADO.net Entity Framework.

I appreciate any assistance you may provide.

1
19
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)
  {
    Console.WriteLine(product.ProductName);
  }
}

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.

23
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"):

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

I appreciate any support.



Related Questions





Related

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