MySQL 连接器 Entity Framework - 未在同一程序集中发现类型

MySQL Connector Entity Framework - Type not discovered in same assembly

我通过 Nuget 安装了 MySQL Connector/NET(特别是 Mysql.Data 和 Mysql.Data.Entities 6.8.3)并按照 MySQL site 上的说明进行操作设置它。这就是我设置 EF 配置的方式:

DbConfiguration.SetConfiguration(new MySqlEFConfiguration());

无论我做什么,我都会收到运行时错误:

System.InvalidOperationException: An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'ApplicationDbContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file.

我尝试了一切,从upgrading/downgrading每个nuget包到摆弄web.config再到改变我的程序集中的东西(例如编译任何CPU 与 x64 等)。经过 6 小时的搜索并尝试修复此错误,我希望这里有人知道如何修复它。如何将 MySqlEFConfigurationApplicationDbContext 放在同一个程序集?

编辑

这是堆栈跟踪:

[InvalidOperationException: An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'ApplicationDbContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.]
   System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForAssembly(Assembly assemblyHint, Type contextTypeHint) +722
   System.Data.Entity.Infrastructure.DependencyResolution.DbConfigurationManager.EnsureLoadedForContext(Type contextType) +38
   System.Data.Entity.DbContext.InitializeLazyInternalContext(IInternalConnection internalConnection, DbCompiledModel model) +36
   System.Data.Entity.DbContext..ctor(String nameOrConnectionString) +59
   CleverShip.MvcApplication.Application_Start() +226

[HttpException (0x80004005): An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'ApplicationDbContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9916673
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): An instance of 'MySqlEFConfiguration' was set but this type was not discovered in the same assembly as the 'ApplicationDbContext' context. Either put the DbConfiguration type in the same assembly as the DbContext type, use DbConfigurationTypeAttribute on the DbContext type to specify the DbConfiguration type, or set the DbConfiguration type in the config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9930568
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

我在使用 MySQL 和 Entity Framework 时也遇到了一些无法解释的问题。

对于它的价值,这是我在 MVC 项目中使用 MonoDevelop 数据库配置类型属性在 EF 上下文文件中声明的方式,它对我有用。

namespace MySQLEFConsole
{
    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class TheClassContext : DbContext

您可以为 MySql 设置新的 DbConfiguration class。此步骤添加 MySql classes 的所有依赖解析器,可以通过三种方式完成:

在上下文中添加 DbConfigurationTypeAttribute class:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

在应用程序启动时调用 DbConfiguration.SetConfiguration(new MySqlEFConfiguration())

在配置文件中设置DbConfiguration类型:

<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">