Click here to Skip to main content
15,923,120 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
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 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;


i need to change the under line text in my run time
Updated 6-Dec-11 17:34pm
Henry Minute 13-May-10 19:34pm    
I added pre tags. changed nothing else.
[no name] 6-Dec-11 23:34pm    
EDIT: added code blocks
Michel [mjbohn] 8-Dec-11 11:04am    
That's quite an old post from 14.05.2010 :-)
How did it pop up again?

There should be three constructors for the ObjectContext class

public Entities() : 
        base("name=Entities", "Entities")

public Entities(string connectionString) : 
        base(connectionString, "Entities")

public Entities(global::System.Data.EntityClient.EntityConnection connection) : 
        base(connection, "Entities")

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());
Share this answer

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

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900