CORE EF The expected type was 'System.String' but the actual value was of type 'System.G

Posted minther

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CORE EF The expected type was 'System.String' but the actual value was of type 'System.G相关的知识,希望对你有一定的参考价值。

【小提示】使用.NET Core EF 开发时,当你的数据库实体中添加了“Id”的字段时,会默认做为主键。

在设置数据库字段类型时如果设置了varchar或者char字段的长度为36时(36是微软GUID的长度),并且数据存储的数据正好是Guid字符串时,在使用EF查询数据库时EF会自动把数据类型转为Guid类型,如果你的数据库实体类中对应的字段正好是string类型时会抛出以下异常:

An exception occurred while reading a database value for property XXX.Id. The expected type was System.String but the actual value was of type System.Guid.

 

1、修改字段类型为Guid

2、修改数据库字段长度

以上两个方法都可以解决该异常

以上是关于CORE EF The expected type was 'System.String' but the actual value was of type 'System.G的主要内容,如果未能解决你的问题,请参考以下文章

EF Core 新特性——Owned Entity Types

EF Core 'The DbFunction 'ÉmpContext.fnGetEmployeeEligibility' 在调用表值函数时具有无效的返回类型 'ÉmpMaster''

EF Core Error:Unable to cast object of type ‘System.DBNull‘ to type ‘System.String‘

Provided id of the wrong type for class pojo.Books. Expected: class java.lang.Integer, got class jav

.NET Core EF框架使用SQL server 2008数据库分页问题:Incorrect syntax near 'OFFSET'. Invalid usage of the

No context type was found in the assembly 'xxx.xxxx'. CodeFirst Ef错误