实体框架 - 数据库首先没有配置 [英] Entity Framework - Database First without config
问题描述
如何使用Database First方法设置连接字符串?没有构造函数重载,它接受一个连接字符串,当我创建一个(在一个单独的部分类中)时,我得到一个无意识代码优先异常。
链接:
- 有没有办法先改变数据库中的连接字符串?关于修改配置文件中的连接字符串(我想避免的)也是因为它会回收进程(在Web应用程序的情况下)
- 如何在没有App.config的情况下使用Entity Framework 。不好,因为它使用ObjectContext,我需要在导入数据库时生成的上下文。
DbContext上有一个构造函数,它需要一个DbConnection,你需要使用一个EntityConnection对象:
SqlConnectionStringBuilder sqlBuilder =新的SqlConnectionStringBuilder();
/ pre>
//设置数据源的属性。
sqlBuilder.DataSource =server name;
sqlBuilder.InitialCatalog =数据库名称;
sqlBuilder.IntegratedSecurity = true;
//构建SqlConnection连接字符串。
string providerString = sqlBuilder.ToString();
var entityBuilder = new EntityConnectionStringBuilder();
//初始化EntityConnectionStringBuilder。
//设置提供者名称。
entityBuilder.Provider =System.Data.SqlClient;
//设置特定于提供者的连接字符串。
entityBuilder.ProviderConnectionString = providerString;
//设置元数据位置。
entityBuilder.Metadata = @res://*/Model1.csdl | res://*/Model1.ssdl | res://*/Model1.msl;
using(var context = new YourDbContext(entityBuilder.ToString())){
// do stuff here
}
要注意的重要事项是元数据部分 - Model1显然需要替换为您的型号名称。
参考: http://msdn.microsoft.com/en- us / library / bb738533.aspx
编辑20/02/2013 22:25
所以作为一个添加,你需要扩展创建的DbContext类与一个部分类,添加一个构造函数来支持上述代码,如下所示:
code> public partial class YourDbContext
{
public YourDbContext(string connection):base(connection){}
}
此类需要与由实体框架向导生成的DbContext相同的命名空间。
I'm developing a class library that deals with an exiting db using EF. I want to avoid the consumer of the class library (and .exe or a web site) to have in the *.config file the Entity connection string. I want the connection string set a run-time.
How do I set the connection string with Database First approach? There is no constructor overload that takes a connection string and when I created one (in a separate partial class) I got an "UnintentionalCodeFirstException".
I have reviewed already the following links:
- Is there a way to change connection string in database first?. Its about modifying the connection string in the config file, which I want to avoid, also because it would recycle the process (in the case of a web app)
- How can l use Entity Framework without App.config. Not good because it uses ObjectContext and I need the context generated when I imported the database.
There is a constructor on DbContext that takes a DbConnection, and you need to use an EntityConnection object for it:
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
// Set the properties for the data source.
sqlBuilder.DataSource = "server name";
sqlBuilder.InitialCatalog = "database name";
sqlBuilder.IntegratedSecurity = true;
// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();
var entityBuilder = new EntityConnectionStringBuilder();
// Initialize the EntityConnectionStringBuilder.
//Set the provider name.
entityBuilder.Provider = "System.Data.SqlClient";
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
using(var context = new YourDbContext(entityBuilder.ToString())){
//do stuff here
}
The important thing to note is the metadata part - "Model1" obviously needs to be replaced for your model name.
Ref: http://msdn.microsoft.com/en-us/library/bb738533.aspx
EDIT 20/02/2013 22:25
So as an addition you'll need to extend the created DbContext class with a partial class that adds a constructor to support the above code, like this:
public partial class YourDbContext
{
public YourDbContext(string connection) : base(connection) {}
}
This class needs to be in the same namespace as the DbContext that is generated by the entity framework wizard.
这篇关于实体框架 - 数据库首先没有配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!