Efcore: dotnet ef migrations remove throws `System.NullReferenceException`

Created on 2 Oct 2019  路  11Comments  路  Source: dotnet/efcore

When trying to remove a migration, which I created yesterday, I'll get the following.

C:\MyProject>dotnet ef migrations remove
Removing migration '20191001140835_DatabaseSetup'.
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(String value)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty property, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(String builderName, IProperty property, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperties(String builderName, IEnumerable`1 properties, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityType(String builderName, IEntityType entityType, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateEntityTypes(String builderName, IReadOnlyList`1 entityTypes, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(String builderName, IModel model, IndentedStringBuilder stringBuilder)
   at Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateSnapshot(String modelSnapshotNamespace, Type contextType, String modelSnapshotName, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir, String rootNamespace, Boolean force, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(String contextType, Boolean force)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration.<>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.

This would not be problem per se, BUT it removes the migration while not changing the snapshot. This makes it out of alignment with the database and thus a show-stopper.

Further technical details

EF Core version: 3.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Windows 10 1903
IDE: CLI

closed-fixed customer-reported type-bug

All 11 comments

Probably duplicate of #18129.

In any case, this issue (like #18129) is lacking enough information for us to effectively reproduce. Please post a runnable project/solution or complete code listing that demonstrates the behavior you are seeing.

Well, nothing simpler than providing my solution:

src.zip

There are some left over references to a local nuget, which should be irrelevant to the DbContext.
I can reupload a more streamlined sample, if neccessary, but I think it's not much to remove here, to get it to build.

Yes, please provide a code sample that works as-is, along with what needs to be done to reproduce the exception.

This here is a minimal repo
RAPS.zip

You'll find the migration assembly in /src/backend/...EFMigrations

It looks like you can work around this by adding .HasColumnType() calls to the properties inside 20190910094842_CreateARISSnapshot.Designer.cs

Still running into this issue with today's 3.1.0 release. Have tried removing a prior migration as well as adding and removing an empty migration- both cause the same symptoms and error with the same stack trace as originally reported here. Trying to figure out how to provide code as the solution is based on https://aspnetzero.com which requires a paid license.

In the meantime, it may be worth noting that if I start with no DB, nuke my migrations and snapshot files, and run add-migration again, everything works as expected though I now have a single consolidated migration. At that point I can remove that migration or any subsequent migrations via remove-migration successfully. Furthermore, the resulting snapshot is identical to the one I can create by running add-migration on the solution without removing prior migrations (this results in an empty migration). This makes me think that the issue is isolated to migrations created with prior versions of EF Core tooling. I think I can safely share this bit from the original snapshot file, before EF Core packages are upgraded to 3.1.0:

modelBuilder .HasAnnotation("ProductVersion", "2.2.4-servicing-10062") .HasAnnotation("Relational:MaxIdentifierLength", 128) .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

If I upgrade EF related packages, subsequently running add-migration will update the ProductVersion to 3.1.0 and column types will be explicitly defined, but I still cannot run remove-migration successfully.

@joelmdev Please open a new issue and provide as much information as you can. It may turn out that we need a runnable repro to fully investigate,

@ajcvickers still working on that with the company that develops the licensed code. I'm having some difficulty reproducing this with a demo or open source project and I've yet to identify why. If I can get a repro on a project that I can share I'll open up a new issue.

@joelmdev did you ever figure out this issue? I'm experiencing it right now

@bfitz22 It was resolved by the ASP.Net Zero/ ASP.NET Boilerplate team within their codebase in our case. I think it had to do with them incorrectly manipulating their migrations by hand to try to work around this issue, but I haven't had the time to confirm.

Was this page helpful?
0 / 5 - 0 ratings