Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet ef migrations remove throws System.NullReferenceException #18178

Closed
glatzert opened this issue Oct 2, 2019 · 11 comments · Fixed by #18289
Closed

dotnet ef migrations remove throws System.NullReferenceException #18178

glatzert opened this issue Oct 2, 2019 · 11 comments · Fixed by #18289
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@glatzert
Copy link

glatzert commented Oct 2, 2019

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

@roji
Copy link
Member

roji commented Oct 2, 2019

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.

@glatzert
Copy link
Author

glatzert commented Oct 2, 2019

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.

@roji
Copy link
Member

roji commented Oct 2, 2019

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

@glatzert
Copy link
Author

glatzert commented Oct 2, 2019

This here is a minimal repo
RAPS.zip

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

@ajcvickers ajcvickers added this to the 3.1.0 milestone Oct 7, 2019
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Oct 8, 2019
@bricelam
Copy link
Contributor

bricelam commented Oct 8, 2019

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

@joelmdev
Copy link

joelmdev commented Dec 3, 2019

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.

@joelmdev
Copy link

joelmdev commented Dec 3, 2019

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.

@ajcvickers
Copy link
Member

@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,

@joelmdev
Copy link

joelmdev commented Dec 9, 2019

@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.

@bfitz22
Copy link

bfitz22 commented Apr 2, 2020

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

@joelmdev
Copy link

joelmdev commented Apr 3, 2020

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants