EF随机看到​​错误的属性类型 [英] EF randomly sees wrong property type

查看:160
本文介绍了EF随机看到​​错误的属性类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们首先在EF 6.1.3和SimpleInjector 3.1.0中使用EF代码,并且随机获得类似于以下内容的异常:

We're using EF code first with EF 6.1.3 and SimpleInjector 3.1.0 and we're randomly getting exceptions similar to this:

位置"上的"IsDeleted"属性无法设置为"System.Int32"值.您必须将此属性设置为'System.Boolean'类型的非空值.
在System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader,Int32序号)
在System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling [TProperty](Int32序数,字符串propertyName,字符串typeName)
at lambda_method(Closure,Shaper)
在System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
在System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
中 在System.Linq.Enumerable.FirstOrDefault [TSource](IEnumerable`1源)上

The 'IsDeleted' property on 'Location' could not be set to a 'System.Int32' value. You must set this property to a non-null value of type 'System.Boolean'.
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.GetPropertyValueWithErrorHandling[TProperty](Int32 ordinal, String propertyName, String typeName)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

在这种情况下,位置表的IsDeleted列为bit/不为null.而且Location实体上的Property绝对是不可为null的布尔值.我已经检查了git存储库中该实体的整个历史记录,并且该属性除布尔值外别无其他,并且服务器上的当前dll是正确的,所以这不是问题是由错误的代码引起的/dll.

In this case the Location table's IsDeleted column is bit / not null. And the Property on the Location entity is definitely a non-nullable boolean. I've checked through the entire history of the entity in our git repository and this property has never been anything else but a boolean, and the current dll on the server is correct, so it's not like the issue is caused by the wrong code/dll.

真正奇怪的是,在大多数情况下,我们的网站都能正常运行,我希望这种情况每次都会发生.奇怪的是,我们已经看到在3个其他实体上也发生了相同类型的错误(看似也是随机的).在每种情况下,它都是不同的属性和数据类型. 例如:

What's really strange is that most of the time our site works just fine, I would expect this the exception to occur every time. What's weirder is that we've seen this same type of error occur on 3 other entities (also seemingly randomly). In each case it was a different property and data type. For example:

"SysGroup"上的"Description"属性无法设置为"System.Boolean"值.您必须将此属性设置为'System.Int32'类型的非空值.

The 'Description' property on 'SysGroup' could not be set to a 'System.Boolean' value. You must set this property to a non-null value of type 'System.Int32'.

"SysInfo"上的"SysGroupId"属性无法设置为"System.String"值.您必须将此属性设置为类型'System.Int32'的非空值.

The 'SysGroupId' property on 'SysInfo' could not be set to a 'System.String' value. You must set this property to a non-null value of type 'System.Int32'.

"BaseEntity"的"Id"属性无法设置为"System.Guid"值.您必须将此属性设置为'System.Int32'类型的非空值.

The 'Id' property on 'BaseEntity' could not be set to a 'System.Guid' value. You must set this property to a non-null value of type 'System.Int32'.

关于最后一个...我们拥有的每个实体都继承自BaseEntity,该实体具有一个名为'Id'的不可空的int属性.我们也不在任何地方使用任何Guid.因此,如果BaseEntity确实在对Id属性使用Guid,则每个地方的每个查询都将失败.最后一个异常发生在代码浏览属性时,如下所示:

About the last one... Every entity we have inherits from BaseEntity, which has a single non-nullable int property named 'Id'. We don't use any Guid's anywhere either. So if BaseEntity was indeed using a Guid for the Id property, then every query everywhere would fail. This last exception happened when the code was navigating a property, like this:

var desc = SysInfoInstance.SysGroup.Description;

有什么想法吗?

Location类本质上是:

The Location class is esentially:

public class Location : 
    BaseEntity
{
    ...
    public virtual bool IsDeleted { get; set; }
}
public abstract class BaseEntity
{
    public virtual int Id { get; set; }
}

推荐答案

出现相同的问题.看起来像某种类型的EF错误连接到存储过程.您在应用程序中使用它们吗?

Had the same problem. Looks like some type of EF bug connected to store procedures. Are you using them in your app?

在我的情况下,问题是,我已经更改了数据库中表的结构(将列添加到中间),而存储过程(从该数据库获取数据)已经添加到EF中.之后,每次尝试调用存储过程时,都会出现相同的异常.异常看起来很奇怪,我浪费了很多时间来找出问题所在.

In my case the problem was, that I've changed the structure (added columns to the middle) of the table in DB, while the stored procedure (which get the data from that DB) was already added to EF. After that the same exception appears each time I've tried to call the stored procedure. The exception looks really weird and I lost a lot of time to clear out whats wrong.

解决方案是将新列移动到表的末尾.它修复了所有问题.看起来EF的存储过程以某种方式保存了数据的结构,并且当表结构发生更改时,它会引发非常奇怪和错误的异常.

The solution was to move the new columns to the end of the table. It fixed everything. Looks like the EF`s store procedures somehow save the structure of data and when the table structure changes it throws very weird and wrong exception.

另一种解决方案是从EF中删除存储过程,然后放回原处.

Another solution was to remove the stored procedure from EF and put it back.

这篇关于EF随机看到​​错误的属性类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆