문제

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!

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top