8

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?

Marko
  • 20,385
  • 13
  • 48
  • 64
Mr. Muh
  • 649
  • 1
  • 9
  • 23
  • I don't believe this is specifically an EF5 problem, but rather that the Designer does not generate a DbContext derived class that includes a constructor for this. You should be able to modify the t4 template to generate the constructor you want. – Erik Funkenbusch Sep 11 '12 at 07:21
  • Have you tried this DbContextConstructor http://msdn.microsoft.com/en-us/library/gg679467%28v=vs.103%29.aspx ? it's supported in EF5. – petro.sidlovskyy Sep 11 '12 at 07:21
  • @petro.sidlovskyy - His problem is that the designer generated context class does not include that constructor, so he can't access it without modifying the context class, but this would get overwritten each time he regenerates the data from the db. – Erik Funkenbusch Sep 11 '12 at 07:23
  • 1
    That's strange. If DbContext is generated as partial class you may just implement constructor with connection string in another file which will not be overridden. – petro.sidlovskyy Sep 11 '12 at 07:25

1 Answers1

18

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) {}
}
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291