如何解决这个异常"Could not find a setter for property 'ProductCode' in class ... "

How to solve this exception "Could not find a setter for property 'ProductCode' in class ... "

我遇到以下异常:

"Could not find a setter for property 'ProductCode'in class ...OrderItem.class "

属性 (ProductCode) 是我的 table 键之一。

看看 属性 在 class 中的声明如何。

public class OrderItem : EntityBase
{
    public virtual short Company { get; set; }
    public virtual int Order { get; set; }
    public virtual string Seri { get; set; }
    public virtual string ProductCode { get; set; }
    public virtual string Crop { get; set; }
 .
 .
 .
 }

下面是我的映射。

public MapOrderItem()
    {
        Table("IPED");
        CompositeId()
            .KeyProperty(c => c.Company, "C_EMP")
            .KeyProperty(c => c.Order, "P_PED")
            .KeyProperty(c => c.Seri, "S_PED")
            .KeyProperty(c => c.ProductCode, "C_PSV");
        Map(c => c.C_CFO).Not.Nullable();
        Map(c => c.AnotherCurrency).Column("V_IPE");
    .
    .
    .
    }

查了和我类似的疑惑,但是解决方案没有解决我的问题。 已经尝试使用

执行映射
(c => c.ProductCode).Access.ReadOnly();
(c => c.ProductCode).Access.Field();
(c => c.ProductCode).ReadOnly();

到运行直接在数据库中查询,没有给我显示任何错误,只有正确的数据。

解决方案

如果您使用 NHibernate 执行 SQL,请验证 sql 中列的别名。该名称应与 class.

中的 属性 完全相同

例如不正确:

select Product_Id as "ProductCode " 
from Products

见别名中的白色space。这可能是您出错的原因。

检查 sql 中所有列的别名。