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

标签 c# entity-framework

另一位开发人员向我们的一个 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/

相关文章:

sql - 将 BIT 发送到 Entity Framework,但 EF 看到 INT

c# - Entity Framework Include() 在复杂查询中不起作用

Entity Framework 生成的用于字符串匹配的 SQL

c# - 如何从组中获取第一项并准备数据类对象

c# - 动态 LINQ 查询中 DateTime.AddMinutes() 的问题

c# - 我可以在 Visual Studio 解决方案中列出所有*不可用*项目吗?

c# - 指定的数据无法解密

c# - 如何使用C#和 Entity Framework 实现SQL Server分页?

c# - DDD实体的Id与现有数据库建模

c# - 如何安排在 Hangfire 中的特定日期运行的作业