MySQL连接器实体框架 - 未在同一程序集中发现类型

时间:2015-03-08 08:23:10

标签: asp.net-mvc entity-framework mysql-connector

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

DbConfiguration.SetConfiguration(new MySqlEFConfiguration());

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

  

System.InvalidOperationException:。的实例   ' MySqlEFConfiguration'已设置,但此类型未在   与' ApplicationDbContext'相同的程序集上下文。要么把   DbConfiguration类型与DbContext类型在同一程序集中使用   DbConfigurationTypeAttribute在DbContext类型上指定   DbConfiguration类型,或在配置中设置DbConfiguration类型   文件。

我尝试了所有,从升级/降级每个nuget包到乱搞web.config到改变我的程序集中的东西(例如使用Any CPU vs x64等编译)。经过6个小时的搜索并尝试修复此错误,我希望有人知道如何修复它。如何将MySqlEFConfiguration带到与ApplicationDbContext相同的程序集中?

修改

这是堆栈跟踪:

[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

1 个答案:

答案 0 :(得分:2)

我和MySQL以及实体框架一直有几个无法解释的问题。

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

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

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

在上下文类中添加DbConfigurationTypeAttribute:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

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

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

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