Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 564
Mr. Muh Avatar asked Sep 11 '12 07:09

Mr. Muh


People also ask

Does EF Core 5 require .NET 5?

EF Core 5.0 requires a . NET Standard 2.1 platform.

What is the latest version of EF?

The next planned stable release is EF Core 8.0, or just EF8, scheduled for November 2023.

Should I use EF core or EF 6?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.


1 Answers

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) {}
}
like image 121
Erik Funkenbusch Avatar answered Sep 21 '22 04:09

Erik Funkenbusch