I am trying to create a discriminator column. This column will contain one of the many available statuses. As in my code, each status has a name, as well as a background color. Each state has the same base class.
Here is my code:
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"; } } }
And here is my mapping:
public class ItemMap : ClassMap<Item> { public ItemMap() { Id(x => x.Id).GeneratedBy.Identity(); DiscriminateSubClassesOnColumn<int>("ItemStatus", 0).AlwaysSelectWithValue(); } }
Essentially, I would like if I set ItemStatus to Status.Foo , then the ItemStatus column ItemStatus have a value of 1. What I have now does not throw any exceptions, but always inserts ItemStatus as 0 .
This is the input code I'm using:
using (var session = sessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { var item = new Item { ItemStatus = Status.Foo }; session.Save(item); transaction.Commit(); var firstItem = session.Get<Item>(1); Console.WriteLine(firstItem.ItemStatus.Name); }
Where can I read this topic using FNH?
Before anyone suggests checking on Google, I searched for a few things, but no where can I find a complete example.
source share