用流利NHibernate的鉴别器 [英] Using discriminator with Fluent NHibernate
问题描述
public class Item
{
public virtual int Id {get;组; }
public virtual Status ItemStatus {get;组; }
}
public abstract class Status
{
private readonly int _id;
public static readonly状态Foo = new FooStatus(1);
public static readonly Status Bar = new BarStatus(2);
public Status()
{
}
保护状态(int id)
{
_id = id;
}
public virtual int Id {get {return _id; }}
public abstract string Name {get; }
public abstract string BackgroundColor {get; }
$ b $ public class FooStatus:Status
{
public FooStatus()
{
}
public FooStatus(int id)
:base(id)
{
public override string Name
{
得到{返回Foo状态; }
}
public override string BackgroundColor
{
get {returnWhite; }
$ b $ public class BarStatus:Status
{
public BarStatus()
{
}
$ b $ public BarStatus(int id)
:base(id)
{
}
public override string Name
{
get {returnBar Status; }
}
public override string BackgroundColor
{
get {returnBlack; }
$ / code $ / pre
$ b $这里是我的映射:public class ItemMap:ClassMap< Item>
{
public ItemMap()
{
Id(x => x.Id).GeneratedBy.Identity();
DiscriminateSubClassesOnColumn< int>(ItemStatus,0).AlwaysSelectWithValue();
$ b $ p
$ b基本上,我想要的是如果我设置ItemStatus
为Status.Foo
,那么ItemStatus
列会有一个值1.我现在不会抛出任何异常,但总是插入ItemStatus
作为0
。
这是我使用的插入代码:
使用(var session = sessionFactory.OpenSession())
使用(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);
$ / code>在哪里可以阅读关于这个主题的使用FNH?
在任何人建议在Google上进行检查之前,我确实搜索了几件事情,但没有在哪里可以找到一个完整的示例。
解决方案您的
SubclassMap
看起来像这样:
public class FooStatusMap:SubclassMap< FooStatus>
{
public FooStatusMap()
{
DiscriminatorValue(1);
$ b $ p $这个被称为每班级表,你是对的,它看起来不像有很多资源。
我相信如果你不'在
SubclassMap
中调用DiscriminatorValue
,NHibernate试图通过查看被映射的子类的名字来判别它匹配鉴别器列中的值。I'm trying to create a discriminator column. This column would hold one of the many statuses available. Like my code will show each status has a name as well as a background color. Each status share 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, what I'd like is that if I set
ItemStatus
toStatus.Foo
then theItemStatus
column would have a value of 1. What I have now doesn't throw any exceptions but it always insertsItemStatus
as0
.This is the inserting 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 up on this topic using FNH?
Before anyone suggests be to check on Google I did search several things but no where can I find a full example.
解决方案Your
SubclassMap
would look something like this:public class FooStatusMap : SubclassMap<FooStatus> { public FooStatusMap() { DiscriminatorValue(1); } }
This is called "table-per-class-hierarchy," and you're right it doesn't look like there are many resources on it out there.
I believe if you don't call
DiscriminatorValue
in aSubclassMap
, NHibernate attempts to discriminate by looking at the name of the subclass being mapped and seeing if it matches up with the value in the discriminator column.这篇关于用流利NHibernate的鉴别器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文