3

我想在一个级别中拥有每个类型的表,在另一个级别中拥有一个用于层次结构的表。可能吗?

描述在这里->

我有这些课程:

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");
    }
}

请帮帮我!

4

1 回答 1

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);
    }
}
于 2015-10-14T14:02:58.643 回答