EF核心删除迁移错误:对象引用未设置为对象的实例

时间:2019-11-18 06:18:25

标签: c# ef-code-first entity-framework-core-3.0

自从我开始使用EF core 3.0之后,每当我想要还原迁移时,我就一直遇到null引用异常。添加迁移已添加,没有任何错误,但是运行删除迁移时,出现空引用异常。尽管已删除了迁移文件,但未删除ContextModelSnapshot.cs文件中的迁移内容,我想这是由于空引用异常导致不允许成功完成删除迁移过程所致。

注意:我的任何实体框架配置中都没有HasComment。

任何人都知道如何解决此错误。

  

System.NullReferenceException:对象引用未设置为对象的实例。      在Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper.Literal(字符串值)      在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GeneratePropertyAnnotations(IProperty属性,IndentedStringBuilder stringBuilder)中      在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.GenerateProperty(String builderName,IProperty属性,IndentedStringBuilder stringBuilder)      在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)中      在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpSnapshotGenerator.Generate(字符串builderName,IModel模型,IndentedStringBuilder stringBuilder)      在Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGenerator.GenerateSnapshot(String modelSnapshotNamespace,Type contextType,String modelSnapshotName,IModel模型)      在Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.RemoveMigration(String projectDir,String rootNamespace,Boolean force,String语言)      在Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.RemoveMigration(String contextType,Boolean force)      在Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigrationImpl(String contextType,Boolean force)处      在Microsoft.EntityFrameworkCore.Design.OperationExecutor.RemoveMigration。<> c__DisplayClass0_0。<。ctor> b__0()      在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase。<> c__DisplayClass3_0`1.b__0()      在Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action操作)   对象引用未设置为对象的实例。

1 个答案:

答案 0 :(得分:0)

不确定当前是否是实际问题,但是当我从EF core 2.x更新到3.x时,我遇到了同样的问题。 EF Core 3.0中有一些重大更改:

https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#microsoftentityframeworkcoredesign-is-now-a-developmentdependency-package

我通过删除该软件包引用并改用Microsoft.EntityFrameworkCore.Tools 3.1.1解决了我的问题。