我希望在一个级别中具有每个类型的表,在另一个级别中具有一个用于层次结构的表。 有可能吗?
描述在这里 - >
我有这些课程:
public class BaseItem
{
public int Id{ get; set; }
}
public class Item : BaseItem
{
}
public class Child1 : Item
{
}
public class Child2 : Item
{
}
我想拥有“BaseItem”和“Item”的表格而不是“Child1”和“Child2”的表格
我试试这个映射:
public class BaseItemMap : ClassMap<BaseItem>
{
public BaseItemMap()
{
Id(p => p.Id).Column("BaseItemId");
}
}
public class ItemMap : SubclassMap<Item>
{
public Item()
{
KeyColumn("BaseItemId");
//note: this line does not compiled
// because "ItemMap" should be of "SubclassMap" and
// I can not use "ClassMap"!
DiscriminateSubClassesOnColumn("ItemType");
}
}
public class Child1Map : SubclassMap<Child1>
{
public BuildingNewsMap()
{
DiscriminatorValue(@"Child1");
}
}
public class Child2Map : SubclassMap<Child2>
{
public BuildingNewsMap()
{
DiscriminatorValue(@"Child2");
}
}
请帮助我!
答案 0 :(得分:1)
public class BaseItem
{
public virtual int Id { get; protected set; }
public virtual DateTime Created { get; set; }
}
public class Item : BaseItem
{
public virtual string Name { get; set; }
}
public class Child1 : Item
{
public virtual int Property1 { get; set; }
}
public class Child2 : Item
{
public virtual int Property2 { get; set; }
}
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
Id(p => p.Id).Column("BaseItemId").GeneratedBy.HiLo("100");
Join("BaseItemTable", join =>
{
join.KeyColumn("BaseItemId");
join.Map(x => x.Created);
});
DiscriminateSubClassesOnColumn("ItemType");
}
}
public class Child1Map : SubclassMap<Child1>
{
public Child1Map()
{
DiscriminatorValue("Child1");
Map(x => x.Property1);
}
}
public class Child2Map : SubclassMap<Child2>
{
public Child2Map()
{
DiscriminatorValue("Child2");
Map(x => x.Property2);
}
}