在NHidernate中将嵌套对象展平为单行

时间:2013-10-23 06:22:48

标签: c# nhibernate nhibernate-mapping

我有一个类嵌套在另一个类中:

public class InnerClass
{
    public string InnerProp1 { get; set; }
    public string InnerProp2 { get; set; }
}

public class OuterClass
{
    public string OuterProp1 { get; set; }
    public string OuterProp2 { get; set; }
    public InnerObject InnerClass { get; set; }
}

我想将它映射到此表:

CREATE TABLE FlatTable
(
    OuterProp1 VARCHAR(20),
    OuterProp2 VARCHAR(20),
    InnerProp1 VARCHAR(20),
    InnerProp2 VARCHAR(20),
)

我尝试过天真的映射

Property(x => x.OuterProp1);
Property(x => x.OuterProp2);
Property(x => x.InnerObject.InnerProp1);
Property(x => x.InnerObject.InnerProp2);

这失败了ArgumentNullException,我怀疑是x.InnerObject为空。

如何创建此映射?

1 个答案:

答案 0 :(得分:2)

使用组件

Property(x => x.OuterProp1);
Property(x => x.OuterProp2);
Component(
    x => x.InnerClass,
    comp =>
    {
        comp.Property(x => x.InnerProp1);
        comp.Property(x => x.InnerProp2);
    });