This is my first question, and I know I should search before asking anything, I am sure that I have done search but I didn't find appropriate information.
I am using code-first approach to implement my Context and my Models, So I have a simple Context like:
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public partial class MultipleContext : DbContext
{
public MariaDBContext(string connection) : base(connection)
{
//Database.SetInitializer<MultipleDBContext>(new MariaDbInitializer());
}
public virtual DbSet<Test> Tests { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Test>().ToTable("test")
.HasKey(e => e.ID);
}
}
and my model:
public partial class Test
{
public int ID { get; set; }
public string Name { get; set; }
public string Family { get; set; }
}
and I have two connectionstrings
like below:
<connectionStrings>
<add name="MariaDBContext" connectionString="server=127.0.0.1;user id=root;password=xx;database=sb1" providerName="MySql.Data.MySqlClient" />
<add name="SqlDBContext" connectionString="Data Source=localhost;Integrated Security=SSPI;Initial Catalog=db1" providerName="System.Data.SqlClient" />
</connectionStrings>
and my EF config :
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.8.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</providers>
</entityFramework>
I want to write to different Db just by changing ConnectionString
like:
MultipleDBContext context = new MultipleDBContext(System.Configuration.ConfigurationManager.ConnectionStrings["MariaDBContext"].ToString());
var xx = context.Tests.Where(x => x.ID > 0).ToList();
context.Tests.Add(new Test()
{
Name = "name",
Family = ""
});
context.SaveChanges();
xx = context.Tests.Where(x => x.ID > 0).ToList();
//Use sql connection
MultipleDBContext sqlContext = new MultipleDBContext (System.Configuration.ConfigurationManager.ConnectionStrings["SqlDBContext"].ToString());
var sqlTest = sqlContext.Tests.Where(x => x.ID > 0).ToList();
sqlContext.Tests.Add(new Test()
{
Name = "name_" + DateTime.Now.Ticks.ToString(),
Family = "family_" + DateTime.Now.Ticks.ToString(),
});
sqlContext.SaveChanges();
sqlTest = sqlContext.Tests.Where(x => x.ID > 0).ToList();
the first context
works fine but the sqlcontext
get the below exception:
An unhandled exception of type 'System.NullReferenceException' occurred in EntityFramework.dll Additional information: Object reference not set to an instance of an object
but if I remove the DbConfigurationType
decoration then the second sqlContext
works fine the first one give below exception:
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll Additional information: Login failed for user 'root'.
I know that is because of DbConfigurationType
which can define in application start or decorate on Context
or defined in config file....
but how can I have this (multiple connectionstrings
and one context)?