MySql和SqlServer是否可以具有EF上下文? [英] Is it possible to have a EF Context for MySql and SqlServer?

查看:158
本文介绍了MySql和SqlServer是否可以具有EF上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体框架上下文,一个用于MySql,一个用于sql.如果我运行该应用程序,则会出现以下错误

I have two entity framework contexts, one for MySql and one for sql. If I run the app I get the following error

The default DbConfiguration instance was used by the Entity Framework before the     'MySqlEFConfiguration' type was discovered. 

但是,如果我确实通过给应用程序赋予Database.SetInitializer<MySqlContext>来发现应用程序上的数据库类型,那么我的常规sql上下文现在正尝试使用mysql提供程序进行连接.

However if I do discover the database type on app start by giving it the Database.SetInitializer<MySqlContext>, my normal sql context is now trying to connect using the mysql provider.

My MySql上下文

My MySql Context

[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ZipCodeContext : DbContext
{
    public DbSet<ZipCode> ZipCodes { get; set; }        
}

我的正常情况

public class MyContext : DbContext
{
    public DbSet<MyClass> MyClasses{get;set;}
}

最后是我的实体框架部分的Web配置

and finally my web config for the entity framework section

 <entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

我希望这已经足够清楚了,我正在把头撞在桌子上

I hope this is clear enough, I'm banging my head on the desk

推荐答案

我的Uow.cs具有2个不同的上下文作为变量.结果,在对象初始化期间,即使我只对第二个上下文感兴趣,第一个上下文的DbConfiguration也会被静默应用.结果,第二个上下文以相同的错误响应,直到我更改了设计.

My Uow.cs had 2 different contexts as variables. As a result during object initialization the first context's DbConfiguration was silently applied even when I was interested only in the second context. As a result the second context have responded with the same error until I have changed the design.

更新

更多详细信息-我已经更新了代码,以对Oracle和MS SQL版本使用相同的上下文.为了提供对应的db配置,我在与上下文的同一程序集中添加了自定义config类,并将其与DbConfigurationType属性一起使用.在您的情况下,可能是这样的:

More details - I have updated the code to use the same Context for Oracle and MS SQL versions. To provide correspondent db configuration I have added custom config class in the same assembly with the context and use it with DbConfigurationType attribute. In your case it could be something like this:

public class CustomDbConfiguration : DbConfiguration
{
    public CustomDbConfiguration()
    {
        if (DbChecker.MySqlInUse)
            SetConfiguration(new MySqlEFConfiguration());
    }
}

,并且两种情况都试图将其应用

and both contexts are trying to apply it

[DbConfigurationType(typeof(CustomDbConfiguration))]
public class ZipCodeContext : DbContext
{
    public DbSet<ZipCode> ZipCodes { get; set; }
}

[DbConfigurationType(typeof(CustomDbConfiguration))]
public class MyContext : DbContext
{
    public DbSet<MyClass> MyClasses { get; set; }
}

这篇关于MySql和SqlServer是否可以具有EF上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆