C# 获取实体框架连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13003797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the Entity Framework Connection String
提问by Craig
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.
我们使用 Entity Framework 5,但还需要使用来自应用程序的普通数据库连接来执行我们需要执行的一些自定义 SQL。
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?
因此,我正在创建一个处理此连接的 DatabaseAccess 类。有没有办法通过检查实体框架连接字符串来填充连接字符串?
So:
所以:
SqlConnection cnn;
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
Can I build that from checking Entity Framework?
我可以通过检查实体框架来构建它吗?
采纳答案by Mark Oreta
Do you mean, can you use a SqlConnection with your EF DbContext?
你的意思是,你能在你的 EF DbContext 中使用 SqlConnection 吗?
The DbContext class has a constructorwhere you can pass in a SqlConnection, and then tell EF whether or not it owns it.
DbContext 类有一个构造函数,您可以在其中传入一个 SqlConnection,然后告诉 EF 它是否拥有它。
var YourContext = new YourContext(SqlConnection, true);
回答by Not loved
You can get the connectionstring used by EF by using the following:
您可以使用以下命令获取 EF 使用的连接字符串:
MyDbContext.Database.Connection.ConnectionString
Or as mark says you can initalise the context with a sqlconnection
或者正如马克所说,您可以使用 sqlconnection 初始化上下文
回答by Wimpie Ratte
Yes you can.
是的你可以。
See herefor 3 options.
请参阅此处了解 3 个选项。
1 - use separate connection string for each
1 - 为每个使用单独的连接字符串
2 - extract it from your entity object (this is what i think you want)
2 - 从你的实体对象中提取它(这是我认为你想要的)
3 - use the entity object to execute your custom SQL
3 - 使用实体对象来执行您的自定义 SQL
Here's how to do nr 2:
以下是如何做 nr 2:
using System.Data.EntityClient;
using System.Data.SqlClient;
...
private string GetADOConnectionString()
{
SalesSyncEntities ctx = new SalesSyncEntities(); //create your entity object here
EntityConnection ec = (EntityConnection)ctx.Connection;
SqlConnection sc = (SqlConnection)ec.StoreConnection; //get the SQLConnection that your entity object would use
string adoConnStr = sc.ConnectionString;
return adoConnStr;
}
回答by JBrooks
Here is a generic way of getting the connection string:
这是获取连接字符串的通用方法:
public string GetConnString<T>(T ent)
where T : ObjectContext
{
EntityConnection ec = (EntityConnection)ent.Connection;
SqlConnection sc = (SqlConnection)ec.StoreConnection;
return sc.ConnectionString;
}
Then to use it would be:
然后使用它是:
MyEntities ent = new MyEntities();
String ConnString = GetConnString(ent);
回答by gbjbaanb
even in EF3 you can use EntityConnectionStringBuilder.
即使在 EF3 中,您也可以使用 EntityConnectionStringBuilder。
EntityConnectionStringBuilder conn =
new EntityConnectionStringBuilder(DBEntities.dbConnection);
SqlConnection s = new SqlConnection(conn.ProviderConnectionString);
回答by Bryan
Here's how to get the connection string in EF 5, EF 6 and EF Core 1/EF 7.
以下是在 EF 5、EF 6 和 EF Core 1/EF 7 中获取连接字符串的方法。
//Entity Framework 5
myContext.Database.Connection.ConnectionString
//Entity Framework 6
myContext.Database.Connection.ConnectionString
//Entity Framework Core 1
myContext.Database.GetDbConnection().ConnectionString
For more details see - http://nodogmablog.bryanhogan.net/2016/04/entity-framework-checking-the-connection-string-of-your-context/
有关更多详细信息,请参阅 - http://nodogmablog.bryanhogan.net/2016/04/entity-framework-checking-the-connection-string-of-your-context/
回答by Rafael Andrs Cspedes Basterio
I had this problem too, to solve the problem you can do this:
我也有这个问题,要解决这个问题,你可以这样做:
SqlConnection con = context.Database.Connection as SqlConnection;
SqlCommand command = new SqlCommand(sql, con);
con.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.HasRows)
{
while (reader.Read())
{
//read your fields
}
reader.NextResult();
}
con.Close();
回答by Rajesh Kanna
Hope it helps..
希望能帮助到你..
private readonly ApplicationDbContext _applicationDbContext;
string connectionString = string.Empty;
public CommonService(ApplicationDbContext applicationDbContext)
{
_applicationDbContext = applicationDbContext;
connectionString = _applicationDbContext.Database.GetDbConnection().ConnectionString;
}
回答by Baahubali
//Entity Framework 5
//实体框架5
myContext.Database.Connection.ConnectionString
//Entity Framework 6
//实体框架6
myContext.Database.Connection.ConnectionString
//Entity Framework Core 1
//实体框架核心1
myContext.Database.GetDbConnection().ConnectionString

