如何使鉴别表更有用

时间:2011-08-01 15:29:20

标签: c# fluent-nhibernate discriminator

我正在尝试从this文章中复制此表格结构。

http://grabbagoftimg.s3.amazonaws.com/nhib_disc_01.PNG

这是我的POCO和映射。

POCO的

public class Item
{
    public virtual int Id { get; set; }
    public virtual Status ItemStatus { get; set; }
}

public abstract class Status
{
    private readonly int _id;
    public static readonly Status Foo = new FooStatus(1);
    public static readonly Status Bar = new BarStatus(2);

    public Status()
    {

    }

    protected Status(int id)
    {
        _id = id;
    }

    public virtual int Id { get { return _id; } }
    public abstract string Name { get; }
    public abstract string BackgroundColor { get; }
}

public class FooStatus : Status
{
    public FooStatus()
    {

    }

    public FooStatus(int id)
        : base(id)
    {

    }

    public override string Name
    {
        get { return "Foo Status"; }
    }

    public override string BackgroundColor
    {
        get { return "White"; }
    }
}

public class BarStatus : Status
{
    public BarStatus()
    {

    }

    public BarStatus(int id)
        : base(id)
    {

    }

    public override string Name
    {
        get { return "Bar Status"; }
    }

    public override string BackgroundColor
    {
        get { return "Black"; }
    }
}

映射:

public class ItemMap : ClassMap<Item>
{
    public ItemMap()
    {
        Id(x => x.Id).GeneratedBy.Identity();
        References(x => x.ItemStatus);
    }
}

public class StatusMap : ClassMap<Status>
{
    public StatusMap()
    {
        Id(x => x.Id).GeneratedBy.Assigned();

        DiscriminateSubClassesOnColumn<int>("ItemStatus", 0);
    }
}

public class FooStatusSubMap : SubclassMap<FooStatus>
{
    public FooStatusSubMap()
    {
        Extends<Status>();
        DiscriminatorValue(1);
    }
}

public class BarStatusSubMap : SubclassMap<BarStatus>
{
    public BarStatusSubMap()
    {
        Extends<Status>();
        DiscriminatorValue(2);
    }
}

这个问题是它生成我的Status表,如下所示:

标识.... ItemStatus

1 ..... 1

2 ..... 2

正如您所看到的,这是无用的,因为两列始终包含相同的值。

如何复制上面链接的图像?

1 个答案:

答案 0 :(得分:3)

这里的关键是,Id和Discriminatorcolumn必须具有相同的列名

public class StatusMap : ClassMap<Status>
{
    public StatusMap()
    {
        Id(x => x.Id, "ItemStatus").GeneratedBy.Assigned();

        DiscriminateSubClassesOnColumn<int>("ItemStatus", 0);
    }
}