2

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?

1 Answer 1

6

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:

<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="MsSqlServerContext" connectionString="MSSQLCONNECTIONSTRING" providerName="System.Data.SqlClient" />
    <add name="MySqlServerContext" connectionString="MYSQLCONNECTIONSTRING" providerName="MySql.Data.MySqlClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <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" />
    </providers>
  </entityFramework>
</configuration>

MsSqlServerContext.cs

public partial class MsSqlServerContext : DbContext
{
    public MsSqlServerContext()
        : base("name=MsSqlServerContext")
    {
        Database.SetInitializer<MsSqlServerContext>(null);
    }

    // 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());
    }
}

MySqlServerContext

public partial class MySqlServerContext : DbContext
{
    public MySqlServerContext()
        : base("name=MySqlServerContext")
    {
        Database.SetInitializer<MySqlServerContext>(null);
    }

    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());
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I am still getting "Keyword not supported. Parametername: multipleactiveresultsets" with those configurations. Somehow EF6 tries to use MySql provider.
Double check the spellings! I struggled with typing issues. See all references of MySqlServerContext and MsSqlServerContext. That was my fault in the beginning
I have solved this by creating DB-First EF6 datasource with vs2010. It uses other code generator(.edmx instead of t4 .tt files) so resulting code is different. And it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.