nHibernate复合键类类型不匹配

时间:2009-08-25 15:31:20

标签: nhibernate composite-key

我有一个带有复合键的遗留表,映射到其他3个表,因为这个表中有其他属性,因为它不是一个简单的映射表,我不能使用多对多集合解决方案映射这个。

以下是我所做的:

<class name="classA" table="A">
<composite-id name="ID" class="AKey">
  <key-many-to-one name="Id_one" class="One" column="Id_one" />
  <key-many-to-one name="Id_two" class="Two" column="Id_two" />
  <key-many-to-one name="Id_three" class="Three" column="Id_three" />
</composite-id>

AKey只是一个包含三个ID的结构,Id_one,Id_two和Id_three都在各自的类中定义为int。

public struct Akey {
    public int Id_one { get; set; }
    public int Id_two { get; set; }
    public int Id_three { get; set; }
}

这个编译很好,但是当我尝试运行它时,它会给我一个错误信息:

NHibernate.QueryException:NHibernate.Criterion.SimpleExpression中的类型不匹配:ID期望类型AKey,实际类型System.Int32

请告知我做错了什么或错过了什么。

非常感谢!

1 个答案:

答案 0 :(得分:5)

如果您打算使用多对多键,您可以使用该类:

public class Akey {
    public virtual One One {get; set;}
    public virtual Two Two {get; set;}
    public virtual Three Three {get; set;}
}

否则,如果你想要Id,你只需将它们映射为classA的属性:

 <composite-id>
     <key-property name="Id_one" column="Id_one" />
     <key-property name="Id_two" column="Id_two" />
     <key-property name="Id_three" column="Id_three" />
 </composite-id>

public class classA {
    public virtual int Id_one {get; set;}
    public virtual int Id_two {get; set;}
    public virtual int Id_three {get; set;}

    // ... rest of props ...
}

或者像你一样复合:

 <composite-id name="ID" class="AKey">
     <key-property name="Id_one" column="Id_one" />
     <key-property name="Id_two" column="Id_two" />
     <key-property name="Id_three" column="Id_three" />
 </composite-id>

public class AKey {
    public virtual int Id_one {get; set;}
    public virtual int Id_two {get; set;}
    public virtual int Id_three {get; set;}
}

public class classA {
    public virtual AKey ID {get; set;}

    // ... rest of props ...
}

最后......

 <composite-id>
   <key-many-to-one name="Id_one" class="One" column="Id_one" />
   <key-many-to-one name="Id_two" class="Two" column="Id_two" />
   <key-many-to-one name="Id_three" class="Three" column="Id_three" />
 </composite-id>

public class classA {
    public virtual One One {get; set;}
    public virtual Two Two {get; set;}
    public virtual Three Three {get; set;}

    // ... rest of props ...
}

是否可以使用结构,因为我在C#中不能胜任它们。