我有两个实体框架上下文,一个用于 MySql,一个用于 sql。如果我运行该应用程序,我会收到以下错误
The default DbConfiguration instance was used by the Entity Framework before the 'MySqlEFConfiguration' type was discovered.
但是,如果我确实发现应用程序上的数据库类型,请先给它Database.SetInitializer<MySqlContext>
,我的正常 sql 上下文现在正在尝试使用 mysql 提供程序进行连接。
我的 MySql 上下文
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ZipCodeContext : DbContext
{
public DbSet<ZipCode> ZipCodes { get; set; }
}
我的正常情况
public class MyContext : DbContext
{
public DbSet<MyClass> MyClasses{get;set;}
}
最后是实体框架部分的网络配置
<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>
我希望这足够清楚,我正在把头撞在桌子上
我的 Uow.cs 有 2 个不同的上下文作为变量。因此,在对象初始化期间,即使我只对第二个上下文感兴趣,也会默默地应用第一个上下文的 DbConfiguration。结果,第二个上下文响应了相同的错误,直到我更改了设计。
update
更多详细信息 - 我已更新代码以对 Oracle 和 MS SQL 版本使用相同的上下文。为了提供相应的数据库配置,我在具有上下文的同一程序集中添加了自定义配置类,并将其与 DbConfigurationType 属性一起使用。在你的情况下,它可能是这样的:
public class CustomDbConfiguration : DbConfiguration
{
public CustomDbConfiguration()
{
if (DbChecker.MySqlInUse)
SetConfiguration(new MySqlEFConfiguration());
}
}
两种情况都在尝试应用它
[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; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)