流畅的NHibernate继承映射

时间:2013-12-05 15:25:23

标签: c# nhibernate inheritance fluent-nhibernate single-table-inheritance

在我的动物等级中

基础一:

public class AnimalMap : ClassMap<Animal>
{
    public AnimalMap()
    {
      Schema("dbo");
      Table("Animals");   

      Id(x => x.Id).Column("ID").GeneratedBy.Identity();
      Map(x => x.FoodClassification).Column("FoodClassification");
      Map(x => x.BirthDate).Column("BirthDate");
      Map(x => x.Family).Column("Family");

      DiscriminateSubClassesOnColumn("ClassType").Not.Nullable();    
    }
}

一个子类:

public class DogMap : SubclassMap<Dog>
{
    public DogMap()
    {
          DiscriminatorValue(@"Dog");
          Map(x => x.Field).Column("Field");
    }
}

所以问题是:

列“ClassType”!= Dog, Animal 应该是对象类型,就像基类一样。没有映射类的每个人都应该有基础(超级)。

如何使其有效?

1 个答案:

答案 0 :(得分:1)

重要的是:只有这样才能支持遗留模式,而动物只能读取

public class SomeAnimal : Animal
{

}

public class AnimalMap : ClassMap<Animal>
{
    public AnimalMap()
    {
      Schema("dbo");
      Table("Animals");   

      Id(x => x.Id).Column("ID").GeneratedBy.Identity();
      Map(x => x.FoodClassification).Column("FoodClassification");
      Map(x => x.BirthDate).Column("BirthDate");
      Map(x => x.Family).Column("Family");

      DiscriminateSubClassesOnColumn().Formula("IIF(classtype = 'dog', 'dog', 'someAnimal')");
    }
}

public class SomeAnimalMap : SubclassMap<SomeAnimal>
{
    public SomeAnimalMap()
    {
          ReadOnly();

          DiscriminatorValue("someAnimal");
          Map(x => x.ClassType).Column("classtype");
    }
}