Use MySQL and MSSQL in Entity Framework 6

entity-framework-6 mysql sql-server


I'm developing a webservice.

Behind this service are two methods: One to get entities from a MySQL Connection and the other to get entities from a MSSQL Server Connection.

I have two connection strings.

I would like to have two contexts, they are completely separated.

But i'm not able to manage this.

Any ideas?

7/7/2015 10:01:45 PM

Accepted Answer

The solution was simple in the end.

Be sure to install MySql Connector/Net on all target systems! I missed this on my target platform.

web.config / app.config:

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <add name="MsSqlServerContext" connectionString="MSSQLCONNECTIONSTRING" providerName="System.Data.SqlClient" />
    <add name="MySqlServerContext" connectionString="MYSQLCONNECTIONSTRING" providerName="MySql.Data.MySqlClient" />
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
        <parameter value="mssqllocaldb" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />


public partial class MsSqlServerContext : DbContext
    public MsSqlServerContext()
        : base("name=MsSqlServerContext")

    // Add DbSets here
    public DbSet<ClassName1> SomeName1 { get; set; }
    public DbSet<ClassName2> SomeName2 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        // Add Mappings here
        modelBuilder.Configurations.Add(new ClassName1Map());
        modelBuilder.Configurations.Add(new ClassName2Map());


public partial class MySqlServerContext : DbContext
    public MySqlServerContext()
        : base("name=MySqlServerContext")

    public DbSet<ClassName3> SomeName3 { get; set; }
    public DbSet<ClassName4> SomeName4 { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        modelBuilder.Configurations.Add(new ClassName3Map());
        modelBuilder.Configurations.Add(new ClassName4Map());
7/9/2015 10:20:35 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow