Question

I have an entity called Product:

  public abstract class Product : Entity
  {
    public long ProductId { get; set; }
    public long Price { get; set; }
    public abstract string Name { get; }
  }

I have a product called Toy

  class Toy : Product
  {
    public override string Name { get { return "Toy Product"; } }
    public string Colour { get; set; }
  }

So effectively, a Toy is a Product. The Product has its own properties, but it also has an abstract Property called "Name". The child knows its own name, so it will override it.

This seemed fine with me, but when I run Add-Migration (Since I am using EF Code-First), the discriminator field is missing. If remove the abstract field, and make the class non-abstract, the discriminator field appears.

Can I make a parent class abstract?

Thanks!

Was it helpful?

Solution

It removed the discriminator, because there's no need for it when you only have a single entity inheriting, since you can't instantiate an abstract class.

If you add another entity that inherits, like Bear:

public class Bear : Product
{
    public override string Name { get { return "Bear Product"; } }
    public string Fur { get; set; }
}

And your discriminator column will come back:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top