arao6
arao6

Reputation: 3376

MySQL Connector Entity Framework - Type not discovered in same assembly

I installed MySQL Connector/NET via Nuget (specifically Mysql.Data and Mysql.Data.Entities 6.8.3) and followed the instructions on the MySQL site to set it up. This is how I am setting up the EF configuration:

DbConfiguration.SetConfiguration(new MySqlEFConfiguration());

No matter what I do, I get a runtime error:

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.

I tried everything, from upgrading/downgrading every nuget package to messing around with web.config to changing things in my assembly (e.g. compiling with Any CPU vs x64 etc). After 6 hours of searching and trying to fix this error, I'm hoping someone here knows how to fix it. How can I bring MySqlEFConfiguration to the same assembly as ApplicationDbContext?

Edit

Here is the stack trace:

[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

Upvotes: 0

Views: 2803

Answers (1)

RNov
RNov

Reputation: 36

I've been having several unexplained issues as well with MySQL and the Entity Framework.

For what it's worth, this is the way I declared in the EF context file in an MVC project with MonoDevelop the database configuration type attribute and it worked for me.

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

You can set the new DbConfiguration class for MySql. This step adds all the dependency resolvers for MySql classes which can be done in three ways:

Adding the DbConfigurationTypeAttribute on the context class:

[DbConfigurationType(typeof(MySqlEFConfiguration))]

Calling DbConfiguration.SetConfiguration(new MySqlEFConfiguration()) at the application startup

Set the DbConfiguration type in the configuration file:

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

Upvotes: 2

Related Questions