另一位开发人员向我们的一个 EF 模型实体 (OrderItem) 添加了一个新的非 null int 属性 (OrderItemSource)。
我们的应用程序在尝试从数据库加载 OrderItems 时抛出异常。
[InvalidOperationException: The 'OrderItemSource' property on 'OrderItem' could not be set to a 'String' value. You must set this property to a non-null value of type 'Int32'. ]
System.Data.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +218 ....
- 该属性在数据库中是 int not null 类型,可以直接在表上访问。
在应用程序端,OrderItemSource 表示为枚举值,并通过以下属性使用分部类进行抽象
public CartItem.SourceType Source { get { return (CartItem.SourceType)OrderItemSource; } set { OrderItemSource = (int)value; } }
- EF 生成的代码看起来不错,因为它创建了一个 Int32 类型的属性。
模型看起来像这样:
<Property Name="OrderItemSource" Type="int" Nullable="false" />
<Property Type="Int32" Name="OrderItemSource" Nullable="false" />
<ScalarProperty Name="OrderItemSource" ColumnName="OrderItemSource" />
有人能明白为什么这可能会引发异常吗?从 DB、EF 到我的部分,这些类型看起来都不错。
感谢任何帮助。
最佳答案
菜鸟时刻 - 如果我检查了正确的数据库那就太好了。 数据库上的类型实际上是 varchar,它与 DB 项目不同步。
关于c# - EF 异常 - InvalidOperationException : The 'x' property on 'y' could not be set to a 'String' value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691291/