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

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

问题描述

我们处于一个混合环境中,我们的应用程序同时使用 ADO.NET 和 Entity Framework.
由于两者都指向同一个物理 SQL 服务器,我们希望从配置文件中删除实体框架连接字符串,然后根据当前的 ADO.NET 连接字符串自动构建字符串.
这使我们避免了开发人员更改 ADO.NET 字符串但忘记更改实体框架连接字符串的错误.

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.

我已阅读此内容,但他们没有回答问题.
如何创建连接字符串在实体框架 6 中以编程方式到 MS SQL?

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?

如果我创建自己的 DbConnection 并将其传递给 DbContext(existingConnection, contextOwnsConnection),则会引发错误上下文正在代码优先模式下使用,代码是从数据库优先或模型的 EDMX 文件生成的第一次开发."

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."

我没有使用 Code First.

I am not using Code First.

https://msdn.microsoft.com/en-us/data/jj680699
这谈到了 EF 6 中的代码库配置,但文章没有显示任何实际更改连接字符串的代码.

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.

更新:更多信息有助于澄清我的问题.
我没有先使用代码,而是想在配置文件之外构建一个连接字符串.
我使用的 DbContext 是 T4 模板正在生成的自动生成的 DbContext 文件的部分类.
我的印象是我需要创建一个继承的 DbConfiguration 类并在该类中执行某些操作,但我发现的唯一示例是使用 Azure.
https://msdn.microsoft.com/en-us/data/jj680699
有一篇关于代码项目的文章讨论了在运行时设置连接字符串,但这篇文章是基于每次创建新实体容器时构建一个连接字符串.
http://www.codeproject.com/Tips/234677/Set-the-connection-string-for-Entity-Framework-at

我希望能够使用我的部分 DbContext 类来创建连接字符串,这样调用者就不必做任何特殊的事情.

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.

更新:RunTime 的工作代码,但 DesignTime 的工作代码
使用@Circular Reference下面列出"发布的代码,我能够在不更改对我的实体类的调用的情况下更改连接字符串,但这不适用于 DesignTime EDMX 文件.

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());
    }
}

现在,如果我能让 DesignTime 正常工作,那就太好了.

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

推荐答案

您收到 Code First 模式异常,因为您正在传递使用 ADO.NET 连接字符串构建的 DbConnection.此连接字符串不包含对元数据文件的引用,因此 EntityFramework 不知道在哪里可以找到它们.

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.

要使用适当的以编程方式设置的连接字符串创建 DbContext,请使用 EntityConnectionStringBuilder 类.

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);

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

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