EF6 Use Existing Db Column as Discriminator

entity-framework entity-framework-6 tph

Popular Answer

As part of the TPH mapping, let EF handle TaskType instead of keeping it in your object. To differentiate types if you're dealing with a base-class collection, use.OfType<PolishedTask>() instead of.Where(x => x.TaskType == TaskType.Polished) The remainder should be handled by EF. In your sub-classes, create a non-mapped property if you do want it to be on the entity. I.e.

public abstract class BaseTask
{
  [NotMapped]
  public abstract TaskType TaskType { get; }
}

public class PolishedTask
{
  [NotMapped]
  public override TaskType TaskType => TaskType.Polished

  // or
  //public override TaskType TaskType
  //{
  //  get { return TaskType.Polished; }
  //}
}
1
9/24/2018 3:27:36 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow