Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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
Posted 13-May-10 14:27pm
Edited 6-Dec-11 18:34pm
v3
Comments
Henry Minute at 13-May-10 19:34pm
   
I added pre tags. changed nothing else.
Eduard Lu at 6-Dec-11 23:34pm
   
EDIT: added code blocks
mjbohn at 8-Dec-11 11:04am
   
That's quite an old post from 14.05.2010 :-)
How did it pop up again?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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();
}
 
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());
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 6 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100