I'm attempting to create a new migration via command line.
Exception message:
Stack trace:
dotnet ef migrations add migration-test-1
Build started...
Build succeeded.
System.NullReferenceException: Object reference not set to an instance of an object.
at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.ApplyDefaultDataTypeMappings(MySqlDefaultDataTypeMappings defaultDataTypeMappings, MySqlConnectionSettings connectionSettings)
at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.Initialize(IDbContextOptions options)
at Microsoft.EntityFrameworkCore.Internal.SingletonOptionsInitializer.EnsureInitialized(IServiceProvider serviceProvider, IDbContextOptions options)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>g__BuildServiceProvider|3()
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext`5..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`8..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`3..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`1..ctor(DbContextOptions options)
at Alugueis.Web.Data.AppDbContext..ctor(DbContextOptions`1 options, IHttpContextAccessor httpContextAccessor, IWebHostEnvironment env) in /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Data/AppDbContext.cs:line 28
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_3.<FindContextTypes>b__11()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
Pomelo MySQL 5.0.0-alpha.2
DotNet v5.0.100
EF Core v5
EF Command Line tools v5.0
MySQL v8.0.22
O.S. Pop-OS 20.04 (based on Ubuntu 20.04)
Connection code:
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
connectionString,
new MySqlServerVersion("8.0.22"),
mySqlOptions => mySqlOptions
.CharSetBehavior(CharSetBehavior.NeverAppend))
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
Generally, migrations should work. I recently posted some sample code and console output for some other issue, where migrations worked as expected: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/1243#issuecomment-726966587
So where to go from here?
First, lets start by creating your migration with the --verbose flag, and post the output (replace sensitive informations with dummy data):
dotnet ef migrations add migration-test-1 --verbose
Also, it will speed-up the investigation process if you clone the current master branch to your local machine and compile (e.g. by running dotnet build) the solution. Then let your project reference all assemblies from the src/EFCore.MySql/bin/Debug/netstandard2.1 directory instead of the Pomelo.EntityFrameworkCore.MySql NuGet package.
Finally, add the migration and post the exception again, because there should now be line numbers in the exception message.
Here is the output with the verbose option
Using project '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj'.
Using startup project '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj'.
Writing '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/obj/Alugueis.Web.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpT0z2MA.tmp /verbosity:quiet /nologo /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj
Writing '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/obj/Alugueis.Web.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpChSfKy.tmp /verbosity:quiet /nologo /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj
Build started...
dotnet build /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj /verbosity:quiet /nologo
/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Data/AppDbContext.cs(23,33): warning CS0169: O campo "AppDbContext._connectionString" nunca 茅 usado [/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj]
/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Data/AppDbContext.cs(22,47): warning CS0169: O campo "AppDbContext._httpContextAccessor" nunca 茅 usado [/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj]
Compila莽茫o com 锚xito.
/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Data/AppDbContext.cs(23,33): warning CS0169: O campo "AppDbContext._connectionString" nunca 茅 usado [/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj]
/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Data/AppDbContext.cs(22,47): warning CS0169: O campo "AppDbContext._httpContextAccessor" nunca 茅 usado [/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Alugueis.Web.csproj]
2 Aviso(s)
0 Erro(s)
Tempo Decorrido 00:01:10.95
Build succeeded.
dotnet exec --depsfile /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/bin/Debug/net5.0/Alugueis.Web.deps.json --additionalprobingpath /home/rafael/.nuget/packages --runtimeconfig /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/bin/Debug/net5.0/Alugueis.Web.runtimeconfig.json /home/rafael/.dotnet/tools/.store/dotnet-ef/5.0.0/dotnet-ef/5.0.0/tools/netcoreapp3.1/any/tools/netcoreapp2.0/any/ef.dll migrations add migration-test-1 --assembly /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/bin/Debug/net5.0/Alugueis.Web.dll --startup-assembly /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/bin/Debug/net5.0/Alugueis.Web.dll --project-dir /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/ --language C# --working-dir /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web --verbose --root-namespace Alugueis.Web
Using assembly 'Alugueis.Web'.
Using startup assembly 'Alugueis.Web'.
Using application base '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/bin/Debug/net5.0'.
Using working directory '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web'.
Using root namespace 'Alugueis.Web'.
Using project directory '/media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'Alugueis.Web'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Found DbContext 'AppDbContext'.
Finding DbContext classes in the project...
System.NullReferenceException: Object reference not set to an instance of an object.
at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.ApplyDefaultDataTypeMappings(MySqlDefaultDataTypeMappings defaultDataTypeMappings, MySqlConnectionSettings connectionSettings)
at Pomelo.EntityFrameworkCore.MySql.Internal.MySqlOptions.Initialize(IDbContextOptions options)
at Microsoft.EntityFrameworkCore.Internal.SingletonOptionsInitializer.EnsureInitialized(IServiceProvider serviceProvider, IDbContextOptions options)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>g__BuildServiceProvider|3()
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.<>c__DisplayClass4_0.<GetOrAdd>b__2(Int64 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.ServiceProviderCache.GetOrAdd(IDbContextOptions options, Boolean providerRequired)
at Microsoft.EntityFrameworkCore.DbContext..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext`5..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`8..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`3..ctor(DbContextOptions options)
at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext`1..ctor(DbContextOptions options)
at Alugueis.Web.Data.AppDbContext..ctor(DbContextOptions`1 options) in /media/rafael/data/dev/csharp/Alugueis/Alugueis.Web/Data/AppDbContext.cs:line 26
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_3.<FindContextTypes>b__11()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.
I'll give it a try using local compiled binaries and post results here.
Thanks
@rafakwolf Thanks for the --verbose log. Unfortunately, it looks fine as far as I can see.
I'll give it a try using local compiled binaries and post results here.
Please do. Once you have the local binaries referenced, you can also just put a Debugger.Launch(); statement at the beginning of the MySqlOptions.ApplyDefaultDataTypeMappings() method (and compile Pomelo.MySql again) and let the JIT debugger jump into the method, when it gets hit.
There should be no scenario, under which a NullReferenceException can occur in this method. But since it does in your case, it will be interesting to find out, how this is achieved.
@lauxjpn Thanks for the help so far ...
FYI using local binaries I had the same result :(
But as a new information I changed my code like so:
var connectionString = Configuration.GetConnectionString("DefaultConnection");
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
connectionString,
new MySqlServerVersion("8.0.22"),
mySqlOptions =>
{
mySqlOptions.CharSetBehavior(CharSetBehavior.NeverAppend);
mySqlOptions.DefaultDataTypeMappings(
mappings => mappings.WithClrDateTime(MySqlDateTimeType.DateTime6)); // <--- added this
})
.EnableSensitiveDataLogging()
.EnableDetailedErrors()
);
then the error starts to happen at the line 188 instead of the line 180 of MySqlOptions.cs file.
FYI using local binaries I had the same result :(
@rafakwolf That was expected and what we wanted, because the primary thing we were after with the local binaries were the line numbers in the exception message, which you now posted:
then the error starts to happen at the line 188 instead of the line 180 of MySqlOptions.cs file.
Thanks for reporting this issue!
There seems to be an error in the handling of version strings in the MySqlServerVersion and MariaDbServerVersion classes, which I will fix shortly.
For a quick workaround, use one of the following calls:
```c#
options.UseMySql(
connectionString,
ServerVersion.FromString("8.0.22-mysql"), // <-- use ServerVersion.FromString() with a "-mysql" suffix instead
mySqlOptions => mySqlOptions
.CharSetBehavior(CharSetBehavior.NeverAppend))
options.UseMySql(
connectionString,
new MySqlServerVersion(new Version(8, 0, 22)), // <-- use new Version() parameter instead
mySqlOptions => mySqlOptions
.CharSetBehavior(CharSetBehavior.NeverAppend))
```
As a side note, be aware that MySQL 8.0.22 has some serious regression bugs (see #1207, #1210 and #1216).
If your queries are running fine for you, than just keep using it. Otherwise, we recommend using 8.0.21 instead, until Oracle fixes these bugs.
The fix is now available in the latest nightly build.
Most helpful comment
@rafakwolf That was expected and what we wanted, because the primary thing we were after with the local binaries were the line numbers in the exception message, which you now posted:
Thanks for reporting this issue!
There seems to be an error in the handling of version strings in the
MySqlServerVersionandMariaDbServerVersionclasses, which I will fix shortly.For a quick workaround, use one of the following calls:
```c#
options.UseMySql(
connectionString,
ServerVersion.FromString("8.0.22-mysql"), // <-- use ServerVersion.FromString() with a "-mysql" suffix instead
mySqlOptions => mySqlOptions
.CharSetBehavior(CharSetBehavior.NeverAppend))
options.UseMySql(
connectionString,
new MySqlServerVersion(new Version(8, 0, 22)), // <-- use new Version() parameter instead
mySqlOptions => mySqlOptions
.CharSetBehavior(CharSetBehavior.NeverAppend))
```
As a side note, be aware that MySQL
8.0.22has some serious regression bugs (see #1207, #1210 and #1216).If your queries are running fine for you, than just keep using it. Otherwise, we recommend using
8.0.21instead, until Oracle fixes these bugs.