Pomelo.EntityFrameworkCore.MySql
Pomelo.EntityFrameworkCore.MySql copied to clipboard
Can't add empty or new migration with Add-Migration
Steps to reproduce
- Have already a migration
- Try to add another one by just using Package Manager Console command => Add-Migration Test
- Or try to add an empty migration
The issue
Supposed to add an empty or a new migration with all it's script to update my database.
If you are seeing an exception, include the full exceptions details (message and stack trace).
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Initialize(ColumnOperation columnOperation, IColumn column, RelationalTypeMapping typeMapping, Boolean isNullable, IEnumerable`1 migrationsAnnotations, Boolean inline)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.InitializeColumnHelper(ColumnOperation columnOperation, IColumn column, Boolean inline)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IColumn source, IColumn target, DiffContext diffContext)+MoveNext()
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.MakeStringColumnsRequiredWithoutUnexpectedDefaultValue(IColumn source, IColumn target, IEnumerable`1 migrationOperations)+MoveNext()
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.SkipRedundantCharSetSpecifyingAlterColumnOperations(IEnumerable`1 migrationOperations)+MoveNext()
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.PostFilterOperations(IEnumerable`1 migrationOperations)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(ITable source, ITable target, DiffContext diffContext)+MoveNext()
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.PostFilterOperations(IEnumerable`1 migrationOperations)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.PostFilterOperations(IEnumerable`1 migrationOperations)+MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
at Pomelo.EntityFrameworkCore.MySql.Migrations.Internal.MySqlMigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
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.
Further technical details
MySQL version: 8.0.32 MySQL Community Server Operating system: Windows 10 22H2 Pomelo.EntityFrameworkCore.MySql version: 7.0.0 Microsoft.AspNetCore.App version: 6.0
Have you tried running it with the -Verbose flag? You can see the log trace for what EF is doing and figure out where its stranding.
Have you tried running it with the -Verbose flag? You can see the log trace for what EF is doing and figure out where its stranding.
Hey ! Yes already and nothing seems to appear clearly. The probelm might come from the package as I'm able to do it with Oracle one.
I have this too. Previous migration is created without a problem, but if I immediately add another migration (with zero change), then this exception is thrown.
I tried with -verbose and got this:
No application service provider was found. Finding DbContext classes in the project... Found DbContext 'ApplicationDbContext'. Using DbContext factory 'DesignTimeDbContextFactory'. Using context 'ApplicationDbContext'. Finding design-time services referenced by assembly 'XXXX'... Finding design-time services referenced by assembly 'XXXX.Infrastructure'... No referenced design-time services were found. Finding design-time services for provider 'Pomelo.EntityFrameworkCore.MySql'... Using design-time services from provider 'Pomelo.EntityFrameworkCore.MySql'. Finding IDesignTimeServices implementations in assembly 'XXXX'... No design-time services were found. 'ApplicationDbContext' disposed. System.NullReferenceException: Object reference not set to an instance of an object.
I think I've found the problem.
I changed my code from:
builder.Property(p => p.ConcurrencyToken)
.HasColumnName("concurrencyToken");
to
builder.Property(p => p.ConcurrencyToken)
.IsRowVersion()
.HasColumnName("concurrencyToken");
And then I can no longer create any migrations AFTER the one that created this:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<byte[]>(
name: "concurrencyToken",
table: "my_table",
type: "timestamp(6)",
rowVersion: true,
nullable: true,
oldClrType: typeof(byte[]),
oldType: "longblob",
oldNullable: true);
}
More info:
My concurrency token type was byte[]. This generated a migration that failed when looking up the mapping type.
I changed my concurrency token to DateTime, and it all works now.
This issue should be fixed by https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/pull/1857 (which we have backported to 8.0.2 with #1869).