MySql Connector 6.8.2 RC, Entity Framework 6 and Code First

c# ef-code-first entity-framework mysql

Question

I recently upgraded to Entity Framework 6 and installed the MySql 6.8.2 RC Connector as it stated there was support for EF6 Code First now.

Keep in mind, prior to the upgrade my project was functioning fine running in connector 6.7.4 and EF 5.

I made the following changes to my web.config

<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
  <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.2.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

<connectionStrings>
<add providerName="MySql.Data.MySqlClient" name="dbcontext" connectionString="Server=localhost; Database=db; Uid=un; Pwd=pw;Convert Zero Datetime=True" />
</connectionStrings>

I also added the following references (.net 4.5 versions)

  1. MySql.Data
  2. MySql.Data.Entity.EF6
  3. MySql.Web

The problem is - the moment the code calls the DBContext i receive:

Object reference not set to an instance of an object.

Again - keep in mind that everything worked prior to the upgrade, so I know its not a code issue, however, maybe I didn't setup the web.config properly?

Also - you may ask, if it worked great prior to the upgrade, why upgrade? Well, supposedly EF6 and the new MySql connector solves some bugs I was running into - so I was hoping to implement it now, while its in development and by the time it moves to production (months from now) I should be able to load in the production version of the 6.8.x connector.

Here is the stack trace in case it helps

[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +85
System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +332
System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) +67
System.Data.Entity.Infrastructure.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k) +63
System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) +72
System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) +260
System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) +89
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +79
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +143
System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +171
System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +594
System.Data.Entity.Internal.InternalContext.Initialize() +31
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39
System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +138
System.Data.Entity.Internal.Linq.InternalSet`1.Include(String path) +41
System.Data.Entity.Infrastructure.DbQuery`1.Include(String path) +142
[edited].Global.Application_BeginRequest(Object sender, EventArgs e) in c:\edited\Global.asax.cs:47
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
1
8
12/16/2013 1:42:16 AM

Popular Answer

I had MySQL EF6 and Migrations up and working when everything was in one MVC project. I split it out into layers (Core[Interfaces/Entitites], Data, Services, and Web) and started getting the same error Loren mentioned.

Figured out that it wasn't picking up the connection string from the MVC app. Turns out that all I had to do was re-create the connection string within the App.config in my Data project (where the DbContext and mappings reside).

These are the steps I took to get everything working:

Step 1) Use NuGet to import MySql.Data.Entities (current version as of this post is 6.8.3.0)

Step 2) Add the following to App.config and/or Web.config:

<connectionStrings>
    <add name="MyDB" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost; port=3306; Initial Catalog=mydb; uid=myuser; pwd=mypass;" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>

Step 3) Set up your DbContext to use MySql:

using MyApp.Core.Entities.Directory;
using MyApp.Data.Mapping;
using System.Data.Entity;

namespace MyApp.Data
{
    [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
    public class MyContext : DbContext
    {
        public MyContext() : this("MyDB") { }
        public MyContext(string connStringName) : base(connStringName) {}
        static MyContext ()
        {
            // static constructors are guaranteed to only fire once per application.
            // I do this here instead of App_Start so I can avoid including EF
            // in my MVC project (I use UnitOfWork/Repository pattern instead)
            DbConfiguration.SetConfiguration(new MySql.Data.Entity.MySqlEFConfiguration());
        }

        public DbSet<Country> Countries { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // I have an abstract base EntityMap class that maps Ids for my entities.
            // It is used as the base for all my class mappings
            modelBuilder.Configurations.AddFromAssembly(typeof(EntityMap<>).Assembly);
            base.OnModelCreating(modelBuilder);
        }
    }
}

Step 4) Set the Default Project to your Data project in the Package Manager Console

Step 5) Use enable-migrations, add-migration, update-database like you normally would

27
1/14/2014 6:04:46 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow