实体框架6设置连接字符串运行时间 [英] Entity Framework 6 set connection string runtime

查看:563
本文介绍了实体框架6设置连接字符串运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们处于一个混合的环境中,我们的应用程序同时使用ADO.NET和Entity Framework。

既然两者都指向同一个物理SQL服务器,我们想删除Entity Framework连接字符串从配置文件中,然后根据当前的ADO.NET连接字符串自动生成字符串。

这样可以避免开发人员更改ADO.NET字符串的错误,但是忘记更改Entity Framework连接字符串。



我已经阅读了,但他们没有回答这个问题。

如何在Entity Framework 6中以MS SQL编程方式创建连接字符串?



如果我创建了自己的DbConnection并将其传递给DbContext(existingConnection,contextOwnsConnection),那么它会抛出一个错误上下文在Code First模式中被使用,代码是从一个用于数据库优先或首次开发的EDMX文件。



我没有使用Code First。



https://msdn.microsoft.com/en-us/data/jj680699

这是关于代码库配置在EF 6,但文章没有显示实际更改连接字符串的任何代码。



更新:更多信息,以帮助澄清我的问题。 >

我不是先使用代码,而是要在配置文件之外构建一个连接字符串。

我正在使用的DbContext是自动生成的DbContext文件的一个部分类T4模板正在生成。

我的印象是,我需要创建一个继承的DbConfiguration类,并在该类中执行某些操作,但我发现的唯一示例是使用Azure。

https://msdn.microsoft.com/en-us/data/jj680699

有一个ar代码项目中关于在运行时设置连接字符串的文章,但文章是基于每次创建一个新的实体容器时构建连接字符串。

http://www.codeproject.com/Tips/234677/Set-the-connection -string-for-Entity-Framework-at



我希望能够使用我的部分DbContext类来创建连接字符串,以便调用者不用做任何特别的事情。



UPDATED:RunTime而不是DesignTime的工作代码

使用@Circular Reference发布的代码我可以更改连接字符串而不改变对我的Entity类的调用,但这不适用于DesignTime EDMX文件。

  public partial class TestEntities:DbContext 
{
public TestEntities():base(GetSqlConnection true b

public static DbConnection GetSqlConnection()
{
//初始化EntityConnectionStringBuilder。
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

var connectionSettings = ConfigurationManager.ConnectionStrings(Current_ADO_Connection_In_Config);

//设置提供者名称。
entityBuilder.Provider = connectionSettings.ProviderName;

//设置特定于提供者的连接字符串。
entityBuilder.ProviderConnectionString = connectionSettings.ConnectionString;

//设置元数据位置。
entityBuilder.Metadata =res://*/Models.TestModel.csdl | res://*/Models.TestModel.ssdl | res://*/Models.TestModel.msl;

返回新的EntityConnection(entityBuilder.ToString());
}
}

现在,如果我可以让DesignTime工作,那么将会很好。

解决方案

您正在获得代码优先模式异常,因为您传递了一个 DbConnection 使用ADO.NET连接字符串构建。这个连接字符串不包括对元数据文件的引用,所以EntityFramework不知道在哪里找到它们。



创建一个 DbContext 以适当的方式设置连接字符串,使用 EntityConnectionStringBuilder 类。

  var entityBuilder = new EntityConnectionStringBuilder(); 

//使用您的ADO.NET连接字符串
entityBuilder.ProviderConnectionString = conString;

//设置元数据位置。
entityBuilder.Metadata = @res://*/Model.csdl | res://*/Model.ssdl | res://*/Model.msl;
var dbContext = new DbContext(entityBuilder.ConnectionString);


We are in a mixed environment where our application is using both ADO.NET and Entity Framework.
Since both are pointing to the same physical SQL server, we would like to remove the Entity Framework connection string from the config file and then auto build the string based on the current ADO.NET connection strings.
This saves us from mistakes where a developer changed the ADO.NET string but forgot to change the Entity Framework connection string.

I have read this but they did not answer the question.
How do I create connection string programmatically to MS SQL in Entity Framework 6?

If I create my own DbConnection and pass that to the DbContext(existingConnection, contextOwnsConnection) then it throws an error "The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development."

I am not using Code First.

https://msdn.microsoft.com/en-us/data/jj680699
This talked about code base configuration in EF 6 but the article does not show any code that actually changed the connection string.

UPDATED: More information to help clarify my question.
I am NOT using code first and would like to build a connection string outside of the config file.
The DbContext I am using is a partial class to the auto generated DbContext file that the T4 template is generating.
I was under the impression that I needed to create an inherited DbConfiguration class and do something in that class but the only example I find is using Azure.
https://msdn.microsoft.com/en-us/data/jj680699
There is an article on Code Project that talks about setting the connection string at runtime but the article is based on building a connection string every time I create a new Entity container.
http://www.codeproject.com/Tips/234677/Set-the-connection-string-for-Entity-Framework-at

I would like to be able to use my partial DbContext class to create the connection string so that the caller does not have to do anything special.

UPDATED: Working code for RunTime but not DesignTime
Using code posted by @Circular Reference "listed below", I was able to change the connection string without changing the calls to my Entity class BUT this does not work for DesignTime EDMX file.

public partial class TestEntities : DbContext
{
    public TestEntities() : base(GetSqlConnection(), true)
    {
    }

    public static DbConnection GetSqlConnection()
    {
        // Initialize the EntityConnectionStringBuilder. 
        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

        var connectionSettings = ConfigurationManager.ConnectionStrings("Current_ADO_Connection_In_Config");

        // Set the provider name. 
        entityBuilder.Provider = connectionSettings.ProviderName;

        // Set the provider-specific connection string. 
        entityBuilder.ProviderConnectionString = connectionSettings.ConnectionString;

        // Set the Metadata location. 
        entityBuilder.Metadata = "res://*/Models.TestModel.csdl|res://*/Models.TestModel.ssdl|res://*/Models.TestModel.msl";

        return new EntityConnection(entityBuilder.ToString());
    }
}

Now if I can just get the DesignTime working then that would be good.

解决方案

You are getting the Code First mode exception because you are passing a DbConnection built with the ADO.NET connection string. This connection string does not include references to metadata files, so EntityFramework does not know where to find them.

To create a DbContext with an appropriate programmatically set connection string, use the EntityConnectionStringBuilder class.

var entityBuilder = new EntityConnectionStringBuilder();

// use your ADO.NET connection string
entityBuilder.ProviderConnectionString = conString;

// Set the Metadata location.
entityBuilder.Metadata = @"res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl";
var dbContext = new DbContext(entityBuilder.ConnectionString);

这篇关于实体框架6设置连接字符串运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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