C# 实体框架运行时连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11368897/
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
Entity Framework runtime connection string
提问by
I'd like to supply the connection string for my database at runtime. I am using the Entity Framework. This is what I have so far
我想在运行时为我的数据库提供连接字符串。我正在使用实体框架。这是我到目前为止
class MyClassDBContext:DbContext
{
public MyClassDBContext(string str) : base(str)
{
this.Database.Connection.ConnectionString = str;
}
}
To use the above code, I tried
要使用上面的代码,我试过
//create connection string
EntityConnectionStringBuilder myConn = new EntityConnectionStringBuilder();
myConn.Provider = "System.Data.SqlClient";
myConn.ProviderConnectionString = "user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30";
//inject the connection string at runtime
MyClassDBContext a = new MyClassDBContext(myConn.ToString())
The above code gave me an error saying "Provider keyword not supported". To attempt to debug this error, I tried the following
上面的代码给了我一个错误,说“不支持提供者关键字”。为了尝试调试此错误,我尝试了以下操作
MyClassDBContext a = new MyClassDBContext("metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30")
Now, I got an error saying "metadata keyword not supported". So I changed my code to
现在,我收到一条错误消息,提示“不支持元数据关键字”。所以我改变了我的代码
MyClassDBContext a = new MyClassDBContext("provider=System.Data.SqlClient;provider connection string=user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30")
Now I got an error saying "provider keyword not supported". So I again changed my code to
现在我收到一条错误消息,提示“不支持提供者关键字”。所以我再次将代码更改为
MyClassDBContext a = new MyClassDBContext("user id=xxxx;password=xxxx;server=localhost;database=xxxx;connection timeout=30")
and now it works!. My question is : how do I specify the provider and metadata at runtime? It looks like only the connection string is being accepted. I am using Entity 4.3.1 from Nuget.
现在它起作用了!我的问题是:如何在运行时指定提供者和元数据?看起来只有连接字符串被接受。我正在使用 Nuget 的 Entity 4.3.1。
Thanks
谢谢
采纳答案by robrich
edmx file based EF require the "Provider" and "Metadata" content. Code-first based EF doesn't require this, requiring only the regular connection string. You could use a SqlConnectionBuilder(instead of EntityConnectionStringBuilder) to build this normal connection string if you'd like. But as you've seen, you need only specify the actual connection details. The Provider and Metadata aren't needed in EF 4.3.1's DbContext Code-first paradigm.
基于 .edmx 文件的 EF 需要“提供者”和“元数据”内容。基于代码优先的 EF 不需要这个,只需要常规的连接字符串。如果您愿意,您可以使用 a SqlConnectionBuilder(而不是EntityConnectionStringBuilder)来构建这个普通的连接字符串。但正如您所见,您只需要指定实际的连接详细信息。EF 4.3.1 的 DbContext 代码优先范例中不需要提供程序和元数据。
回答by HatSoft
The EntityConnectionStringBuilder class can be used to to specify provider and metadata at runtime
EntityConnectionStringBuilder 类可用于在运行时指定提供程序和元数据
e.g.
var entityConnectionStringBuilder= new EntityConnectionStringBuilder(); entityConnectionStringBuilder.Provider = "System.Data.SqlClient"; entityConnectionStringBuilder.Metadata = "res:///Example.csdl|res:///Example.ssdl|res://*/Example.msl";
例如
var entityConnectionStringBuilder= new EntityConnectionStringBuilder(); entityConnectionStringBuilder.Provider = "System.Data.SqlClient"; entityConnectionStringBuilder.Metadata = "res:// /Example.csdl|res:///Example.ssdl|res://*/Example.msl";
Please see for more on CSDL, SSDL & MSDLin Metadata
请参阅元数据中有关CSDL、SSDL 和 MSDL 的更多信息
回答by kam lung chan
I followed this link
我跟着这个链接
and also this one
还有这个
How to use EF Code-First without an app.conf file?
如何在没有 app.conf 文件的情况下使用 EF Code-First?
Basically what I do is almost like you, create a constructor with a string and calling the base.
基本上我所做的几乎和你一样,用一个字符串创建一个构造函数并调用基数。
But I also set the provider in this constructor.
但我也在这个构造函数中设置了提供者。
here's a example
这是一个例子
public Context(string ConnectionString) : base(ConnectionString) {
Database.DefaultConnectionFactory = new SqlCeConnectionFactory("Oracle.DataAccess.Client");
}
That way you can specify the provider. And you won't get the provider keyword error since you don't need to specify in the connection string
这样你就可以指定提供者。并且您不会收到 provider 关键字错误,因为您不需要在连接字符串中指定
Here's how I call it.
这就是我的称呼。
var dbCont = new ClassLibrary1.Models.Context("DATA SOURCE=xxx;PASSWORD=xxx;USER ID=xxx");
Hope that helps took me long time to find it
希望能帮助我花了很长时间才找到它
回答by David McClelland
Building on HatSoft's answer:
基于HatSoft 的回答:
var entityConnectionStringBuilder= new EntityConnectionStringBuilder();
entityConnectionStringBuilder.Provider = "System.Data.SqlClient";
entityConnectionStringBuilder.ProviderConnectionString = <your SQL Server connection string>;
entityConnectionStringBuilder.Metadata = "res://*";
MyClassDBContext a = new MyClassDBContext(entityConnectionStringBuilder.ToString());
回答by adobrzyc
It's old question but maybe it will be useful for someone
这是个老问题,但也许对某人有用
var provider = (DbProviderFactory)System.Data.Entity.DbConfiguration
.DependencyResolver
.GetService(typeof(DbProviderFactory), "invariant provider name");
var conn = provider.CreateConnection();
//conn.ConnectionString = "sample connection string";
DbInterception.Dispatch.Connection.SetConnectionString(conn, new DbConnectionPropertyInterceptionContext<string>()
.WithValue("sample connection string"));
return new SampleDbContext(conn,true);

