更新此记录不适用于nHibernate

时间:2012-09-25 16:27:09

标签: c# nhibernate insert has-many

我有以下2个映射:

internal RulesEngineHeaderMap()
{
    Table("LIOEP023");

    CompositeId()
        .KeyProperty(x => x.CompanyCode, "CONO23")
        .KeyProperty(x => x.RuleID, "RLID23");
    Map(x => x.RuleGroup, "RGRP23")
        .Length(30)
        .Not.Nullable();
    Map(x => x.RuleDescription, "RLDS23")
        .Length(50)
        .Not.Nullable();
    Map(x => x.Expression, "EXPR23")
        .Length(2500)
        .Not.Nullable();
    HasMany(x => x.RuleVariables)
        .KeyColumns.Add("CONO24", "RLID24");
}

internal RulesEngineVariableDetailMap()
{
    Table("LIOEP024");
    CompositeId()
        .KeyProperty(x => x.CompanyCode, "CONO24")
        .KeyProperty(x => x.RuleID, "RLID24")
        .KeyProperty(x => x.Name, "RVAR24");
    Map(x => x.Type, "VTYP24")
        .Not.Nullable();
    Map(x => x.Description, "VDES24")
        .Not.Nullable();
    Map(x => x.Required, "RMAN24")
        .Not.Nullable();
    References(x => x.RuleHeader)
        .Columns("CONO24", "RLID24")
        .NotFound.Ignore()
        .Nullable();
}

当我尝试将新对象添加到RulesEngineHeader的RuleVariables IList并更新时,我收到错误

Batch update returned unexpected row count from update; 
  actual row count: 0; expected: 1

当我查看我的SQL时,我看到了这一点:

SELECT rulevariab0_.CONO24 as CONO1_1_,
    rulevariab0_.RLID24 as RLID2_1_,
    rulevariab0_.RVAR24 as RVAR3_1_,
    rulevariab0_.CONO24 as CONO1_1_0_,
    rulevariab0_.RLID24 as RLID2_1_0_,
    rulevariab0_.RVAR24 as RVAR3_1_0_,
    rulevariab0_.VTYP24 as VTYP4_1_0_,
    rulevariab0_.VDES24 as VDES5_1_0_,
    rulevariab0_.RMAN24 as RMAN6_1_0_
FROM   LIOEP024 rulevariab0_
WHERE  rulevariab0_.CONO24 = 'LO' /* @p0 */     <-- KEY FIELD
    and rulevariab0_.RLID24 = 'Test' /* @p1 */  <-- KEY FIELD

where子句只包含我定义的3个关键字段中的2个,因此它查找记录并尝试更新,但是更新如下所示:

UPDATE LIOEP024
SET    CONO24 = 'LO' /* @p0_0 */,
    RLID24 = 'Test' /* @p1_0 */
WHERE  CONO24 = 'LO' /* @p2_0 */         <-- KEY FIELD
    AND RLID24 = 'Test' /* @p3_0 */      <-- KEY FIELD
    AND RVAR24 = 'Some Name' /* @p4_0 */ <-- KEY FIELD

如您所见,它使用了所有3个关键字段。它应该在这里做一个插入,但由于上面的选择找到了记录,它认为它应该进行更新,返回0记录。

我在这里做错了什么想法?

1 个答案:

答案 0 :(得分:1)

您正在使用RuleVariable的复合键。 NHibernate无法知道您的RuleVariable是新实例还是现有实例。如果您使用生成的身份识别身份,那么它可以通过检查身份证是否为&gt;来判断。 0.

你需要告诉NHibernate你的新RuleVariable是一个新实例,即。你需要在保存Header之前保持RuleVariable,这样做,调用session.Save(变量)