Fluent NHibernate inheritance mapping

617 views Asked by At

In my hierarchy of animals

Base one:

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

One subclass:

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

So the question is:

Where column "ClassType" != Dog , Animal should be object type, like base one. Each one who has no mapping class should have base(super) one.

How to make it works?

1

There are 1 answers

4
Firo On BEST ANSWER

important: only do this to support legacy schemas and animal is readonly

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