NibblyPig
NibblyPig

Reputation: 52942

EF won't let me specify the connection string

The way that it auto-generates the classes, it doesn't take a connection string as a parameter - although the generates code passes one to the base class. I can edited the template myself, but isn't there a better way, as I may regenerate the model (maybe even delete & re-create) and I don't want it to affect the template.

//------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated from a template.
//
//    Manual changes to this file may cause unexpected behavior in your application.
//    Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

public partial class MyEntities : DbContext
{
    public MyEntities()
        : base("<Connection string>")
    {
    }
...

I am using a DB first approach. It all works fine but now I've created an exact copy of the database and I need to be able to switch between the two.

Upvotes: 3

Views: 109

Answers (1)

qujck
qujck

Reputation: 14580

You should be able to define a partial class that sits beside your generated class (in a separate file). This partial class can have the second constructor:

public partial class MyEntities : DbContext
{
    public MyEntities(string connectionstring)
        : base(connectionstring)
    {
    }
}

Upvotes: 5

Related Questions