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?

5
  • 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. Commented Dec 5, 2013 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 Commented Dec 5, 2013 at 16:07
  • What would the discriminator value be for the other types? Commented Dec 5, 2013 at 16:16
  • some other word like 'animal' but not dog... or null or empty string. whatever, but they should be Animal class type Commented Dec 8, 2013 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. Commented Dec 8, 2013 at 23:40

1 Answer 1

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");
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

see the solution, but question is how to avoid this one?
Avoid specifying all subclass values in the Formula or the additional class SomeAnimal?
additional class SomeAnimal
you can with DiscriminateSubClassesOnColumn("", "someAnimal").Formula(...);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.