如何在EntityFramework中更改连接字符串

如何在EntityFramework中更改连接字符串

本文介绍了如何在EntityFramework中更改连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我使用第三方为数据库(sql)和Oracle建立了一个实体框架.但要使用相同的数据库模式,并在SSDL.schema中进行所有更改,并在Web.config中添加新的连接字符串
和entity.design.cs为两个数据库共享(当我手动更改代码中的连接字符串时,成功率为100%).我需要在运行时更改此文件中的连接的所有内容.有办法做到吗?

entity.designer.cs
中我的代码的示例

Hi all
i make an entity framework for a database (sql) and another one for Oracle using third party. but to the same db schema and all my changing in the SSDL.schema and adding a new connection string in Web.config
and the entity.design.cs shared for both database (100% success when i change the connection string by hand in the code ). all what i need to change the connection in this file in the run time . is there a way to make it ?

example for my code in entity.designer.cs

public partial class entity: ObjectContext
   {
       public entity() : base(name=connetionstring, context)
       {
           this.ContextOptions.LazyLoadingEnabled = true;
           OnContextCreated();
       }

    }


我需要在运行时更改下划线文本.


i need to change the under line text in my run time

推荐答案

public Entities() :
        base("name=Entities", "Entities")
{
    this.OnContextCreated();
}

public Entities(string connectionString) :
        base(connectionString, "Entities")
{
    this.OnContextCreated();
}

public Entities(global::System.Data.EntityClient.EntityConnection connection) :
        base(connection, "Entities")
{
    this.OnContextCreated();
}



传递连接字符串或创建EntityConnection对象



Pass in the connection string or create an EntityConnection object

private static EntityConnection GetConnection()
{
    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
    entityBuilder.ProviderConnectionString = ConfigurationManager.ConnectionStrings["xxx"].ConnectionString;
    entityBuilder.Provider = "System.Data.SqlClient";
    entityBuilder.Metadata = @"res://*/xxx.csdl|res://*/xxx.ssdl|res://*/xxx.msl";

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




这篇关于如何在EntityFramework中更改连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 05:03