Mr. Muh
Mr. Muh

Reputation: 649

EF 5.0 & dynamic connection string?

One thing is driving me crazy right now.

My (Database-first) EF-Model needs a dynamic connection string (IP-Adress of Server might change once in a while).

So in older EF-Versions you could pass a connection-string via constructor, but that is not possible in 5.0 as is seems.

What I have read so far, you could change your datatemplate, but that will be overwritten each time you re-generate your model etc., so not the best way to do it.

Another thing is the SQLConnectionFactory, but that does not seem to work at all (Database.DefaultConnectionFactory = new SqlConnectionFactory( ... ) seems to be ignored completely).

What would be the right approach for that?

Upvotes: 8

Views: 5067

Answers (1)

Erik Funkenbusch
Erik Funkenbusch

Reputation: 93424

As petro mentions, you could create a partial class with the constructor you want.

For instance:

public partial class MyContext : DbContext
{
    public MyContext(string connectionString) : base(connectionString) {}
}

Upvotes: 18

Related Questions