This 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()< br /> {
}
protected Status(int id)
{
_id = id;
}
< br /> 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()
{< br />
}
public BarStatus(int id)
: base(id)
{
}
< br /> public override string Name
{
get {return "Bar Status"; }
}
public override string BackgroundColor
{
get {return "Black"; }
}
}
This is my mapping:
public class ItemMap: ClassMap
{
public ItemMap()
{
Id(x => x.Id).GeneratedBy.Identity();
DiscriminateSubClassesOnColumn("ItemStatus", 0).AlwaysSelectWithValue();
}
}
Basically, what I want is if I set ItemStatus to Status. Foo, then the value of the ItemStatus column is 1. What I have now No exception is thrown, but it always inserts ItemStatus as 0.
This is the insertion code I am 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- (1);< br /> Console.WriteLine(firstItem.ItemStatus.Name);
}
Where can I use FNH to read this topic?
Before anyone suggests to check Google, I did search for a few things, but there is no complete example where I can find it.
public class FooStatusMap: SubclassMap
{
public FooStatusMap()
{
DiscriminatorValue(1);
}
}
This is called “the table for each class Hierarchy”, and you are right, it seems that there are not many resources on it.
I believe that if you don’t call DiscriminatorValue in SubclassMap, NHibernate will try to view it by looking at the name of the mapped subclass Whether it matches the value in the discriminator column to distinguish.
I am trying to create a discriminator column. This column will contain one of many available states. Just like mine The code is the same, each state will display a name and a background color. Each state shares the same base class.
This is my code:
public class Item
{
public virtual int Id {get; set; }
public virtual Status ItemStatus {get; set; }
}
< br />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< br /> {
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"; }
}
}
This is my mapping:
public class ItemMap: ClassMap
{
public ItemMap()
{
Id(x => x.Id).GeneratedBy.Identity ();
DiscriminateSubClassesOnColumn("ItemStatus", 0).AlwaysSelectWithValue();
}
}
Basically, I want The thing is, if I set the ItemStatus to Status.Foo, then the value of the ItemStatus column is 1. The one I have now does not throw any exception, but it always inserts the ItemStatus as 0.
This Is the insert code I am 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- (1);
Console.WriteLine(firstItem.I temStatus.Name);
}
Where can I use FNH to read this topic?
Before anyone suggests to check Google, I did search for a few things, but there is no complete example where I can find it.
Your SubclassMap looks like this:
public class FooStatusMap: SubclassMap
{
public FooStatusMap()
{
DiscriminatorValue(1);
}
}
This is called “table hierarchy per class”, and you are right, it seems It doesn’t have many resources on it.
I believe that if you don’t call DiscriminatorValue in SubclassMap, NHibernate will try to distinguish by looking at the name of the mapped subclass and see if it matches the value in the discriminator column.