0

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?

Roar
  • 2,117
  • 4
  • 24
  • 39
  • I don't think it's possible, if you had a class of type `Giraffe : Animal`, how would the column value be persisted if you never specified a discriminator value for giraffe. – Matthew Dec 05 '13 at 15:44
  • but what should to do if i don't need classification for everyone who is not a dog? i mean each?, even giraffe, should be an animal but not a giraffe – Roar Dec 05 '13 at 16:07
  • What would the discriminator value be for the other types? – Matthew Dec 05 '13 at 16:16
  • some other word like 'animal' but not dog... or null or empty string. whatever, but they should be Animal class type – Roar Dec 08 '13 at 17:48
  • Try to make a common abstract base type (like `BaseAnimal`), and have both `Animal` and `Dog` derive from it. Then you would set-up 2 subclass mappings. – Matthew Dec 08 '13 at 23:40

1 Answers1

1

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");
    }
}
Firo
  • 30,626
  • 4
  • 55
  • 94