Home > Software engineering >  Adding migration doesn't work and gives no useful error message
Adding migration doesn't work and gives no useful error message

Time:01-01

I'm using EF 6.0.12 and when adding migrations to my project, basically this happens:

Visual Studio:

PM> add-migration Initial
Build started...
Build failed.

CLI:

D:\myproject\db>dotnet-ef migrations add Init
Build started...
Build succeeded.
Unable to create an object of type 'RequestContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Which is as useful as a brick in my face. It gets me really annoyed. And I have no clue on how to solve this as there's no useful error message...
Now, I use these packages in my project:

  • Microsoft.EntityFrameworkCore Version 6.0.12
  • Microsoft.EntityFrameworkCore.Tools Version 6.0.12
  • Pomelo.EntityFrameworkCore.MySql Version 6.0.2

And my DBContext is inside a Class library, and is used by a Web API. Which works quite well, but I now need migrations.
The problem is that my database is MariaDB and the MySQL package I need does not support .NET 7.0, nor do I use 7.0 so the EF7 packages are off-limits. But my problem is that I just don't know how I can even start finding the cause of this problem. So, where do I start to try and make this work?


I've tried to solve things by migrating the project to .NET 7.0 and now use the latest packages:

  • Microsoft.EntityFrameworkCore 7.0.1
  • Microsoft.EntityFrameworkCore.Tools 7.0.1
  • MySql.EntityFrameworkCore 6.0.7

These are all the latest versions. And Pomelo.EntityFrameworkCore.MySql has been removed. Still, it changes nothing about the error I get.
Some more changes made to the code and finally got past this error in the CLI and VS. I now have this exception:

PM> add-migration Katje-Init
Build started...
Build succeeded.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.MissingMethodException: Method not found: 'System.String Microsoft.EntityFrameworkCore.Diagnostics.RelationalStrings.get_NoConnectionOrConnectionString()'.
   at MySql.EntityFrameworkCore.Internal.MySQLOptions.GetConnectionSettings(MySQLOptionsExtension relationalOptions)
   at MySql.EntityFrameworkCore.Internal.MySQLOptions.Initialize(IDbContextOptions options)
   at Microsoft.EntityFrameworkCore.Internal.SingletonOptionsInitializer.EnsureInitialized(IServiceProvider serviceProvider, IDbContextOptions options)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<GetOrAdd>g__BuildServiceProvider|4_1(IDbContextOptions _, ValueTuple`2 arguments)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c.<GetOrAdd>b__4_0(IDbContextOptions contextOptions, ValueTuple`2 tuples)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd[TArg](TKey key, Func`3 valueFactory, TArg factoryArgument)
   at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
   at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
   at RequestDatabase.RequestContext..ctor(DbContextOptions`1 options) in D:\Projects\DB\Datamodel.cs:line 16
Etc...

The CLI gives the same exception now. Not sure how I fixed the previous error, but it's still very unclear. So again, what is the best approach to solve these complex errors?

CodePudding user response:

The last error shows that there is breaking change (usually sign of a version mismatch between the packages). As in this answer - switch from Oracle MySQL package to Pomelo preview one for .NET 7.

  • Related