none
How to change Connection in EDMX designer RRS feed

  • Question

  • Hi guys

    I have a few db connection strings in my web.config that all point to the same database with the same credentials.  I'd like to delete the unnecessary entries and update a few EDMX files to point to the remaining connection string in my web.config, but this option appears greyed out in the designer.

    Any idea how I can do this?

    Tks


    Steven

    Friday, September 13, 2013 12:40 PM

Answers

  • Hello,

    If I understand correctly, you want to change the entity connection at runtime.

    If I have misunderstood, please let me know.

    In my opinion, we can try it like below:

    1.Define a copy of the myOBJEntities in the connectionstring table and define it like this:

    <add name="myOBJEntitiesTempl" connectionString="metadata=res://*/myOBJ.csdl|res://*/myOBJ.ssdl|res://*/myOBJ.msl;provider=System.Data.SqlClient;provider connection string=&quot;{0}&quot;" providerName="System.Data.EntityClient" /> 

    2. Then in the code get this from ConfigurationManager.ConnectionStrings and do a string.Format on it with the connection string to in database. Like this:

    string conn = string.Format(ConfigurationManager.ConnectionStrings["myOBJEntitiesTempl"].ConnectionString, databaseConnection);
    
    using(myOBJEntities context = new myOBJEntities(conn))
    
    {
    
      //.... 
    
    }
    

    There is also possible for us to modify the ((EntityConnection)context.Connection).StoreConnection.ConnectionString, but remember, you have to do this as soon as your context is created.

    using(myOBJEntities context = new myOBJEntities(conn))
    
    {
    
      ((EntityConnection)context.Connection).StoreConnection.ConnectionString = databaseConnection;
    
    }
    

    On the question where we should store it, we could add additional connectionstrings to the connectionStrings section of the file and databaseConnection in the examples above from there.

    Thanks&Regards.


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, September 16, 2013 2:41 AM
    Moderator