实体框架 - 数据库首先没有配置 [英] Entity Framework - Database First without config

查看:117
本文介绍了实体框架 - 数据库首先没有配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用EF处理现有数据库的类库。我想避免类库(和.exe或网站)的消费者在* .config文件中具有Entity连接字符串。我想要连接字符串设置一个运行时间。



如何使用Database First方法设置连接字符串?没有构造函数重载,它接受一个连接字符串,当我创建一个(在一个单独的部分类中)时,我得到一个无意识代码优先异常。



链接:




解决方案

DbContext上有一个构造函数,它需要一个DbConnection,你需要使用一个EntityConnection对象:

  SqlConnectionStringBuilder sqlBuilder =新的SqlConnectionStringBuilder(); 

//设置数据源的属性。
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
}
/ pre>

要注意的重要事项是元数据部分 - 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:

解决方案

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆