Yes: You can prepare a type of DbConnection that can be passed to a DbContext that was created by the base provider and has the correct connection string.
So, to reach this connection string in CODE ... see below
<connectionStrings> <add name="MyCTX" connectionString="Data Source=localhost;Initial Catalog=MYDB ;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" /> </connectionStrings>
remember that DBContext has an overloaded constructor
public DbContext(DbConnection existingConnection, bool contextOwnsConnection)
So you just need Dbconnection built by the base factory provider. Cm
public interface IDbConnectionFactory
which is implanted with types of the 3rd type:
System.Data.Entity.Infrastructure.SqlCeConnectionFactory System.Data.Entity.Infrastructure.LocalDbConnectionFactory System.Data.Entity.InfrastructureSqlConnectionFactory
So, here is an example using the SQLConnectionFActory. This returns a DBConnection. Which can be passed to DBContext. You can repeat / change or make a variable on your program break. For the other 2 providers.
public DbConnection GetSqlConn4DbName(string dataSource, string dbName) { var sqlConnStringBuilder = new SqlConnectionStringBuilder(); sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource; sqlConnStringBuilder.IntegratedSecurity = true; sqlConnStringBuilder.MultipleActiveResultSets = true;
phil soady
source share