获取实体框架连接字符串 [英] Get the Entity Framework Connection String
问题描述
我们使用Entity Framework 5,但是还需要从应用程序中使用一些需要执行的自定义SQL的常规数据库连接。
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.
所以,我正在创建一个处理此连接的DatabaseAccess类。有没有办法通过检查实体框架连接字符串来填充连接字符串?
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?
所以:
SqlConnection cnn;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
我可以通过检查Entity Framework来构建它吗?
Can I build that from checking Entity Framework?
推荐答案
你的意思是,你可以使用SqlConnection EF DbContext?
Do you mean, can you use a SqlConnection with your EF DbContext?
DbContext类有一个构造函数,您可以在其中传入SqlConnection,然后告诉EF是否拥有它。
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);
这篇关于获取实体框架连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!