[英]Class mapping with NHibernate
我有一个普通的数据库表(名为DBFoo):
| PropertyA | PropertyB| PropertyC | PropertyD | PropertyE |
PropertyA,PropertyB和PropertyC是键的一部分。
为此,在我的程序中,我具有以下类结构:
public class Foo
{
public virtual SubFoo SubFoo { get; set; }
public virtual string PropertyC { get; set; }
public virtual string PropertyD { get; set; }
public virtual string PropertyE { get; set; }
}
public class SubFoo
{
public virtual string PropertyA { get; set; }
public virtual string PropertyB { get; set; }
}
现在,我正在尝试创建映射文件:
...
<class name="Foo" table="DBFoo">
<composite-id>
// here I Need to define the mapping for the SubFoo properties PropertyA and PropertyB
<key-property name="PropertyC" column="PropertyC"/>
</composite-id>
<property name="PropertyD" column="PropertyD"/>
<property name="PropertyE" column="PropertyE"/>
</class>
....
有人知道如何为PropertyA和PropertyB定义键属性吗?
预先感谢您的帮助
可能不是一个好主意,但是您可以尝试生成一个FooIdentifier
类来装饰SubFoo
类,以提供对PropertyA
, PropertyB
和PropertyC
直接访问。
public class FooIdentifier
{
private SubFoo InnerSubFoo { get; set; }
public FooIdentifier(SubFoo subFoo, string propertyC)
{
this.InnerSubFoo = subFoo
this.PropertyC = propertyC;
}
public virtual string PropertyA
{
get
{
return SubFoo.PropertyA;
}
set
{
SubFoo.PropertyA = value;
}
}
public virtual string PropertyB
{
get
{
return SubFoo.PropertyB;
}
set
{
SubFoo.PropertyB = value;
}
}
public virtual string PropertyC { get; set; }
}
然后,映射文件可能类似于:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="Foo" table="Foo" lazy="true" >
<composite-id name="FooIdentifier" class="FooIdentifier">
<key-property name="PropertyA" column="PropertyA" />
<key-property name="PropertyB" column="PropertyB" />
<key-property name="PropertyC" column="PropertyB" />
</composite-id>
<property name="PropertyD" column="PropertyD" type="String" />
<property name="PropertyE" column="PropertyE" type="String" />
</class>
</hibernate-mapping>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.