2

Let us consider the entity class listed as

public NerdDinnerEntities() : base("name=NerdDinnerEntities", "NerdDinnerEntities")
        {

            this.ContextOptions.LazyLoadingEnabled = true;

            OnContextCreated();
        }

        /// <summary>
        /// Initialize a new NerdDinnerEntities object.
        /// </summary>
        public NerdDinnerEntities(string connectionString) : base(connectionString, "NerdDinnerEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;

            OnContextCreated();
        }



        /// <summary>
        /// Initialize a new NerdDinnerEntities object.
        /// </summary>
        public NerdDinnerEntities(EntityConnection connection) : base(connection, "NerdDinnerEntities")
        {
            this.ContextOptions.LazyLoadingEnabled = true;
            OnContextCreated();
        }  

and these being read as in web config file as

<add name="NerdDinnerEntities" 
    connectionString="metadata=res://*/Models.NerdDinner.csdl|res://*/Models.NerdDinner.ssdl|res://*/Models.NerdDinner.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=*;
    Initial Catalog=db;User ID=*;Password=****;
    MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

and my problem is i need to change the database name from db to db1 through code with out change in web config file..

as enitity as a class name called changedatabase() .. but i do not know how to use it...

and my another question is it possible to add a new dll in to this project and change the connectionstring meta data there by when ever the connection is opened there by it will come to that dll and change the database name only and return with that database...

Waiting for your valuable commands and sugessions

1 Answer 1

1
public class MyEntities : NerdDinnerEntities
{
  public MyEntities() : base(GetConnectionString())
  {
  }

  private string GetConnectionString()
  {
    var connectionString = System.Configuration.ConfigurationManager.
        ConnectionStrings["connectionStringName"].ConnectionString;
    var builder = new System.Data.Common.DbConnectionStringBuilder();
    builder.ConnectionString = connectionString;
    var internalConnectionString = builder["provider connection string"].ToString();
    var newConnectionString = internalConnectionString.Replace("oldDBName", "newDBName");
    builder["provider connection string"] = newConnectionString;
    return builder.ConnectionString;
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

thanks for posting ! i do not know where to use this code! can u say me!
If you want this change to be permanent then you can replace the connection string in the web.config file, and yes, you can do it from a dll.
ya i want it through dll... i need to create a library file in which inherits dbcontext and place this coding in it ... has i want to change anything in web.config file... give me some tips reg it
the above is my entity class since this concept is new to me i do not know how to proceed with dll ? need ur guidelines /......
In your inherited class (from NerdDinnerEntities), add this code in the parameterless constructor and call your base class constructor that gets a connection string as a paremeter
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.