NHibernate的复合键类的类型不匹配(nHibernate Composite Key Cla

2019-08-02 11:28发布

我有映射到其它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

请我做了什么错误或遗漏提出建议。

多谢!

Answer 1:

如果你要使用钥匙多到一个你会把类:

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#胜任他们。



文章来源: nHibernate Composite Key Class Type Mismatch