EntityFramwork using multiple databases / joins

I have the following problem. I am using Entity Framework 6, and I want to be able to change the database used at runtime, or at least I want to check the connection information when entering in the Parameters. My problem is that we want to support MySql and LocalDB v12.0, so just exchanging the connection string here does not help - I need to exchange ExecutionStrategy and ConnectionFactory.

EF seems to block all configurations, so I cannot change it at runtime, is there a desktop for this? At the moment, I tried to create several DbConfigurations and get the context for each configuration with the definition of [DbConfigurationType(typeof(LocalDbConfigruation))] .

I expected this to fail, but I would like to try :)

Maybe there is someone who can help me with some tips and tricks.

+5
source share
2 answers

Well, the problem is now resolved. I am currently working with DbConnections.

 public MyContext() : base(ConnectionManager.Connection, true) { Database.SetInitializer<MyContext>(new MyContextInitializer()); Configuration.ProxyCreationEnabled = false; } public MyContext(DbConnection connection) : base(connection, true) { Database.SetInitializer<MyContext>(new MyContextInitializer()); Configuration.ProxyCreationEnabled = false; } 

I am creating a DbConnection in a special class, I think it would be impractical to post the code here. But basically this is something like this:

 DbConnection conn = null; switch (Type) { case ConnectionType.LocalDB: conn = DbProviderFactories.GetFactory("System.Data.SqlClient").CreateConnection(); break; case ConnectionType.MySql: conn = DbProviderFactories.GetFactory("MySql.Data.MySqlClient").CreateConnection(); break; default: throw new System.InvalidOperationException(); } conn.ConnectionString = "Add provider specific connection string here"; 

Then you just need to give the code in context. In my case, I have a ConnectionManager, from where I read "defaul connection" when calling MyContext (), and there is a second Ctor that I call for "test connections".

0
source

There is another option that uses the underlying context. In the example below, I am using the MSSQL connection and the Oracle connection. You can expand the underlying context for any type of database connection you want. This method opens up a ton of other great features, but it should also work for your situation.

Basecontext.cs

 using System.Data.Entity; namespace MultipleConnections.Models { public class BaseContext<TContext> : DbContext where TContext : DbContext { static BaseContext() { Database.SetInitializer<TContext>(null); } public BaseContext(string connectionString = "Name=MSSQLEntities") : base(connectionString) {} } } 

MSSQLModel.cs

 using System.Data.Entity; namespace MultipleConnections.Models { // Extending Base Context will use default MSSQLEntities connection public class MSSQLContext : BaseContext<MSSQLContext> { ...apply DbSet<> and other loveliness... } } 

OracleModel.cs

 using System.Data.Entity; namespace MultipleConnections.Models { // Extending Base Context public class OracleContext : BaseContext<OracleContext> { // Declare Oracle connection in Constructor to override default // MSSQL connection public OracleContext() : base("Name=OracleEntities") { } ...apply DbSet<> and other loveliness... } } 
+1
source

Source: https://habr.com/ru/post/1212962/


All Articles