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

Posted RickLee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解决EF迁移数据报No mapping to a relational type can be found for property 'xxx.FieldName' with the 相关的知识,希望对你有一定的参考价值。

.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之前,需要将新变动的模型暂时复原,待不再报错后再修改到所需要的结构。

报This application has no explicit mapping for /error, so you are seeing this as a fallback解决方法

【现象】SpringBoot项目中访问已经存在的url时,报如下错误:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun Dec 04 12:05:36 CST 2022

There was an unexpected error (type=Not Found, status=404).

【解决方法】

SpringBoot项目中的解决方法

启动类中添加scanBasePackages,具体举例如下:

修改前为:

@SpringBootApplication
public class DemoApplication 

   public static void main(String[] args) 
      SpringApplication.run(DemoApplication.class, args);
   

修改后为:

@SpringBootApplication(scanBasePackages="com.digest")
public class DemoApplication 

   public static void main(String[] args) 
      SpringApplication.run(DemoApplication.class, args);
   

以上是关于解决EF迁移数据报No mapping to a relational type can be found for property 'xxx.FieldName' with the 的主要内容,如果未能解决你的问题,请参考以下文章

hdfs追加解决报错:Failed to replace a bad datanode on the existing pipeline due to no more goo......

报This application has no explicit mapping for /error, so you are seeing this as a fallback解决方法

报This application has no explicit mapping for /error, so you are seeing this as a fallback解决方法

springboot启动报错 Failed to configure a DataSource: 'url' attribute is not specified and no emb

EF TO MYSQL 无法查询中文的解决方法

项目启动报错Failed to configure a DataSource: 'url' attribute is not specified and no embedde(代码片段