Reputation: 1484
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="Data Source=*;
Initial Catalog=db;User ID=*;Password=****;
MultipleActiveResultSets=True"" 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
Upvotes: 2
Views: 4038
Reputation: 13296
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;
}
}
Upvotes: 1