3

I'm trying to use MySQL database (ef code first approach, VS 2013 professional). I follow these instructions.

My problem is that after I enable migrations I cannot do a first migration. I use command Add-migration migrationName and I got an error:

"System.NullReferenceException: Object reference not set to an instance of an object. at MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection) at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) at MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection) ...

Object reference not set to an instance of an object.".

I tried to solved it f.e. in this way. But now I found this:

"This can be caused by not specifying a required parameter for a scenario that you are using. For example specifying a connection string without specifying the provider name."

So the problem is probably in my connenctions string.

I have my connections string from VS > Server Explorer there is "active" connection to my database. I viewed the connection properties and copied a connection string into my web.config.

In my project are references to MySql.Data, MySql.Data.Entity.EF6 and MySql.Web.

Anyone have any ideas what should I do?

Grant Winney
  • 61,140
  • 9
  • 100
  • 152
Fotomen
  • 131
  • 2
  • 8
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Preston Guillot Dec 23 '15 at 00:13
  • A System.NullReferenceException is an error during migration I see this error in Package Manager Console, so I cannot insert a breakpoint in my code... – Fotomen Dec 23 '15 at 12:35

2 Answers2

9

I've been here before at least twice for newly setup EF+MySQL projects, which give me the exact same error and stack trace as OP had, when trying to enable code first migrations. Both times it's taken me ages to find the solution that finally worked.

Do make sure your DbContext class has the following attribute:

[DbConfigurationType(typeof(MySqlEFConfiguration))]
class MyDbContext : DbContext
{
    // ...
}
Thomas Hilbert
  • 3,438
  • 2
  • 11
  • 32
5

I solved my problem by copying my connection string into my context class.

public MyContextClass()
    : base("Server=myServer;Port=3306;Database=db_name;Uid=userName;Pwd=password") {}

First migrations starts. After Update-database command I have another problem Specified key was too long; max key length is 767 bytes, but the problem with System.NullReferenceException is solved.

Kroltan
  • 4,424
  • 4
  • 31
  • 52
Fotomen
  • 131
  • 2
  • 8
  • I have seen that max key length thing. I know it is a hack but if you go into the migration file (MigrationName.cs) and find where it has the `maxlength: 256` change `256` to `255` and that should work. – Mutmatt Jan 18 '16 at 19:28