context mysql,MySql和SqlServer是否有EF上下文?

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.

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

My MySql Context

[DbConfigurationType(typeof(MySqlEFConfiguration))]

public class ZipCodeContext : DbContext

{

public DbSet ZipCodes { get; set; }

}

My Normal Context

public class MyContext : DbContext

{

public DbSet MyClasses{get;set;}

}

and finally my web config for the entity framework section

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

解决方案

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.

update

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 ZipCodes { get; set; }

}

[DbConfigurationType(typeof(CustomDbConfiguration))]

public class MyContext : DbContext

{

public DbSet MyClasses { get; set; }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值