将同一个表的2行映射为一个对象

时间:2011-08-11 14:08:12

标签: nhibernate nhibernate-mapping fluent-nhibernate-mapping

在遗留数据库模型之后,我得到了类:

public enum ChildType
{
    Part1,
    Part2,
}

public class Child
{
    public virtual int Id { get; set; }

    public virtual Parent Parent { get; set; }
    public virtual ChildType Type { get; set; }

    public virtual bool Flag1 { get; set; }
    public virtual bool Flag2 { get; set; }

    public virtual char[] Data { get; set; }

    public Child()
    {
        Data = new char[1000];
    }
}

public class Parent
{
    public virtual int Id { get; set; }

    ICollection<Child> Childs { get; set; }
}

每次我需要做

        Child usedChild;
        if (index < 1000)
        {
            usedChild = parent.Childs.First(x => x.Type == ChildType.Part1);
        }
        else
        {
            usedChild = parent.Childs.First(x => x.Type == ChildType.Part2);
            index -= 1000;
        }
        usedChild.Data[index] = value;

我想

public class Child
{
    public virtual int Id { get; set; }

    public virtual Parent Parent { get; set; }

    public virtual bool Flag1 { get; set; }
    public virtual bool Flag2 { get; set; }

    public virtual char[] Data { get; set; }

    public Child()
    {
        Data = new char[2000];
    }
}

这有可能以某种方式吗?类模型和映射是免费的,db-schema已修复

更新:表格结构

Table Parents
(
  parent_id SERIAL NOT NULL,
  primary key(id),
)

Table Childs
(
    Id SERIAL NOT NULL,

    childs_parentid INT,
    childs_rowtype INT,  // 0, 1

    childs_Flag1 INT,  // 0, 1
    childs_Flag2 INT,  // 0, 1

    childs_data CHARACTER VARYING(1000)

    PRIMARY KEY (Id)
)

每个父行总共有2个子行,一个childs_rowtype = 0一个childs_rowtype = 1从不使用id列,除childs_data之外的所有列对于2个子行完全相同。

0 个答案:

没有答案