如何在EntityFramework中更改连接字符串? [英] how to change the connection string in the EntityFramework ?

查看:181
本文介绍了如何在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

推荐答案

ObjectContext类应该有三个构造函数

There should be three constructors for the ObjectContext class

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




您可以通过ObjectContext实例掌握EntityConnection.

ObjectContextInstance.Connection-然后将其强制转换为EntityConnection.

然后到EntityConnection的此实例,获取storeConnection

EntityConnectionInstance.StoreConnection-这为您提供了基础的连接字符串.

希望对您有所帮助!.

问候,
-Vinayak
Hi,

You can get hold of the EntityConnection by ObjectContext instance.

ObjectContextInstance.Connection - and then type cast this to EntityConnection.

Then To this instance of EntityConnection, Get the storeConnection

EntityConnectionInstance.StoreConnection - this gives you the underlying connection string.

I hope this helps!.

Regards,
-Vinayak


设置Entity Framework在运行时的连接字符串 [ ^ ]


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

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