gpt4 book ai didi

c# - EF 异常 - InvalidOperationException : The 'x' property on 'y' could not be set to a 'String' value

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:15 25 4
gpt4 key购买 nike

另一位开发人员向我们的一个 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,它与数据库项目不同步。

关于c# - EF 异常 - InvalidOperationException : The 'x' property on 'y' could not be set to a 'String' value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21691291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com