简体   繁体   中英

Fluent NHibernate inheritance mapping

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM