nHibernate复合键类类型不匹配 [英] nHibernate Composite Key Class Type Mismatch
问题描述
我有一个带有组合键的遗留表,该组合键已映射到其他3个表,因为该表中具有其他属性,因为它不是简单的映射表,所以不能使用多对多集解决方案对此进行映射.
I have a legacy table with composite keys that are mapped to 3 other tables, since this table have other attributes in it, since it is not a simple mapping table, I can't use the many-to-many set solution to map this.
以下是我所做的:
<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.
AKey is merely a struct that holds the three ids, and Id_one, Id_two, and Id_three are all defined as int in their respective class.
public struct Akey {
public int Id_one { get; set; }
public int Id_two { get; set; }
public int Id_three { get; set; }
}
这可以很好地编译,但是当我尝试运行它时,它给了我一条错误消息:
This compiles fine, but when I try to run it, it gives me an error message:
NHibernate.QueryException:NHibernate.Criterion.SimpleExpression中的类型不匹配:ID预期类型为AKey,实际类型为System.Int32
NHibernate.QueryException : Type mismatch in NHibernate.Criterion.SimpleExpression: ID expected type AKey, actual type System.Int32
请告知我做错了什么或错过了什么.
Please advise on what I have done wrong or missed.
感谢一堆!
推荐答案
如果要使用多对一"键,则可以放置该类:
If you are going to use key-many-to-one you would put the class:
public class Akey {
public virtual One One {get; set;}
public virtual Two Two {get; set;}
public virtual Three Three {get; set;}
}
否则,如果您想要ID,则只需将其映射为classA的属性即可.
Otherwise if you want the Id you just map them as properties of 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 ...
}
或者像您一样拥有复合物:
Or as a composite like you have:
<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#中不胜任.
Going to punt on whether you can use a struct because I'm not competent on them in C#.
这篇关于nHibernate复合键类类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!