解决EF迁移数据报No mapping to a relational type can be found for property 'xxx.FieldName' with the CLR type 'string'.错误一例

.net Core 3.1 使用EFCore迁移命令

Remove-Migration

返回信息
Build started...
Build succeeded.
Removing migration '20230514091109_Update20230514'.
Reverting model snapshot.
Done.

使用“0”个参数调用“Remove”时发生异常:“给定关键字不在字典中。”

(有知道如何解决此问题的朋友请给出方案,谢谢。)

发生了报错,此后
Add-Migration Update20230515

报下边的错误,

Build started...
Build succeeded.
System.InvalidOperationException: No mapping to a relational type can be found for property 'xxx.FieldName' with the CLR type 'string'.
at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, IProperty property)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Diff(IProperty source, IProperty target, DiffContext diffContext)+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(TableMapping source, TableMapping target, DiffContext diffContext)+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.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IModel source, IModel 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)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
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)

No mapping to a relational type can be found for property 'xxx.FieldName' with the CLR type 'string'.

各种百度,bing,未果。

后来删除了Migrations目录下的

MySqlDbContextModelSnapshot.cs

再次执行迁移命令
Add-Migration Update20230515

执行顺利成功,并重新生成了MySqlDbContextModelSnapshot.cs。

但是新的迁移文件20230515172127_Update20230515.cs中也将所有的表全部作为新增表生成了命令。

打开此文件,清空Up和Down方法中的所有迁移命令。清空后如下:

protected override void Up(MigrationBuilder migrationBuilder)

{

}

protected override void Down(MigrationBuilder migrationBuilder)

{

}

执行Update-database

顺利执行完成。

测试故障是否解决。

为某个模型增加一个字段,然后再次执行迁移命令

Add-Migration Update20230515A

Build started...
Build succeeded.
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 3.1.12 initialized 'MySqlDbContext' using provider 'Pomelo.EntityFrameworkCore.MySql' with options: None
To undo this action, use Remove-Migration.

报错没有再出现。问题解决。

=====================================================

注意事项:在删除MySqlDbContextModelSnapshot.cs后重新执行迁移命令Add-Migration之前,需要将新变动的模型暂时复原,待不再报错后再修改到所需要的结构。

posted @ 2023-05-16 01:48  RickLee  阅读(151)  评论(0编辑  收藏  举报