How to make discriminator table more useful
I'm trying to replicate this table structure from this article.
Here are my POCO's and mappings.
POCO's
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"; }
}
}
Mappings:
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 Ba开发者_StackOverflow中文版rStatusSubMap : SubclassMap<BarStatus>
{
public BarStatusSubMap()
{
Extends<Status>();
DiscriminatorValue(2);
}
}
The issue with this is that it generates my Status
table like this:
Id....ItemStatus
1.....1
2.....2
As you can see this is rather useless as both columns always contain the same values.
How can I replicate the image that I linked above?
the key here is, that Id and Discriminatorcolumn must have the same column name
public class StatusMap : ClassMap<Status>
{
public StatusMap()
{
Id(x => x.Id, "ItemStatus").GeneratedBy.Assigned();
DiscriminateSubClassesOnColumn<int>("ItemStatus", 0);
}
}
精彩评论