ASP.Net MVC: Dynamically send the Connection string to the Entity Framework Context on DB First approach
Dynamically send the connection string to the Entity Framework context.
In some situation we need to pass on the connection string dynamically when needed. But one more thing that the both DB should have same structure.
as in case of my project i have more then one connection string inside the web.config. when user try to login he need to select the site name he wants to login and based on that the user is validated.
<connectionStrings>
<clear />
<add name="Site1" connectionString="metadata=res://*/HarmonySITContext.csdl|res://*/HarmonySITContext.ssdl|res://*/HarmonySITContext.msl;provider=System.Data.SqlClient;provider connection string="data source=sqlservername;initial catalog=databasename;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="Site2" connectionString="metadata=res://*/HarmonySITContext.csdl|res://*/HarmonySITContext.ssdl|res://*/HarmonySITContext.msl;provider=System.Data.SqlClient;provider connection string="data source=sqlservername;initial catalog=databasename;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
You need to generate edmx file from the Db you want to connect and then it will automatically generate the context class. You need to create a new context class with the same class name as generated context and inherit the generated context. in that generated context you need to specify the connection string variable and pass that to the base.
namespace App.Name.Data
{
public partial class AppNameContext
{
public AppNameContext(string connectionString) //:base()
: base(connectionString)
{
}
}
}
I think this will help. :)
In some situation we need to pass on the connection string dynamically when needed. But one more thing that the both DB should have same structure.
as in case of my project i have more then one connection string inside the web.config. when user try to login he need to select the site name he wants to login and based on that the user is validated.
<connectionStrings>
<clear />
<add name="Site1" connectionString="metadata=res://*/HarmonySITContext.csdl|res://*/HarmonySITContext.ssdl|res://*/HarmonySITContext.msl;provider=System.Data.SqlClient;provider connection string="data source=sqlservername;initial catalog=databasename;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="Site2" connectionString="metadata=res://*/HarmonySITContext.csdl|res://*/HarmonySITContext.ssdl|res://*/HarmonySITContext.msl;provider=System.Data.SqlClient;provider connection string="data source=sqlservername;initial catalog=databasename;persist security info=True;user id=sa;password=sa;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
You need to generate edmx file from the Db you want to connect and then it will automatically generate the context class. You need to create a new context class with the same class name as generated context and inherit the generated context. in that generated context you need to specify the connection string variable and pass that to the base.
namespace App.Name.Data
{
public partial class AppNameContext
{
public AppNameContext(string connectionString) //:base()
: base(connectionString)
{
}
}
}
I think this will help. :)
Comments
Post a Comment