Entity Framework 6 - Set Connection String at Runtime
-
I created an EF6 data context by following [this](http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx). I need to change the connection string at runtime. I found [this article](https://www.codeproject.com/Tips/234677/Set-the-connection-string-for-Entity-Framework-at) but my data context does not have an overloaded contstructer:
public partial class MyEntities : DbContext
{
public MyEntities()
: base("name=MyEntities")
{
}What am I doing wrong? How to I set the EF data Context connection string at runtime??
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I created an EF6 data context by following [this](http://www.entityframeworktutorial.net/EntityFramework5/create-dbcontext-in-entity-framework5.aspx). I need to change the connection string at runtime. I found [this article](https://www.codeproject.com/Tips/234677/Set-the-connection-string-for-Entity-Framework-at) but my data context does not have an overloaded contstructer:
public partial class MyEntities : DbContext
{
public MyEntities()
: base("name=MyEntities")
{
}What am I doing wrong? How to I set the EF data Context connection string at runtime??
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Add the required constructor overloads to your context:
public partial class MyEntities : DbContext
{
public MyEntities() : base("name=MyEntities")
{
}public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString) { } public MyEntities(DbConnection existingConnection, bool contextOwnsConnection) : base(existingConnection, contextOwnsConnection) { }
EDIT: If you're using the
.tt
file to generate the context from the database, you'll probably need to add the extra overloads in a separate file; that's why the class is declared aspartial
.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer