Get the Entity Framework Connection String

c# entity-framework

Question

We use Entity Framework 5, but have a requirement to ALSO use a normal database connection from the application for some custom SQL we need to perform.

So, I am creating a DatabaseAccess class which handles this connection. Is there a way that I can populate the connection string, by checking the Entity Framework connection string?

So:

SqlConnection cnn;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"

Can I build that from checking Entity Framework?

1
36
10/22/2012 1:23:23 AM

Accepted Answer

Do you mean, can you use a SqlConnection with your EF DbContext?

The DbContext class has a constructor where you can pass in a SqlConnection, and then tell EF whether or not it owns it.

var YourContext = new YourContext(SqlConnection, true);
8
5/11/2013 1:09:21 AM

Popular Answer

You can get the connectionstring used by EF by using the following:

MyDbContext.Database.Connection.ConnectionString

Or as mark says you can initalise the context with a sqlconnection



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