TPT inheritance with non-abstract base class in Code First

江枫思渺然 提交于 2020-01-02 18:16:09

问题


I want to create a TPT mapping where the base class itself holds information and the derived classes just extend it.

Basically, I've got a base class, and a few derived classes, and I'm wondering how to handle their mapping. Here's a ridiculously simplified example:

[Table("Task")]
public class Task : TaskBase
{
    // No properties... This is just a "Task", not either an internal nor external one.
}

[Table("InternalTask")]
public class InternalTask : TaskBase
{
    public int UserId { get; set; }

    public virtual User User { get; set; }
}

[Table("ExternalTask")]
public class ExternalTask : TaskBase
{
    public int CustomerId { get; set; }

    public virtual Customer Customer { get; set; }
}

public abstract class TaskBase : Entity
{
    /// <summary>Gets or sets the identifier of this item.</summary>
    public int Id { get; set; }

    [...]

    public string Subject { get; set; }

    public virtual User UserCompletedBy { get; set; }
    public virtual User UserLockedBy { get; set; }
    public virtual User UserOwner { get; set; }

    public virtual ICollection<TaskBase> ChildTasks { get; set; }
    public virtual ICollection<TaskAttachment> Attachments { get; set; }
}

Now Entity Framework generates some weird SQL schema with Customer_Id fields and User_Id fields, although I've specified them correctly(?). Hence I was wondering what the correct approach for such a schema is.

Thank you!

来源:https://stackoverflow.com/questions/36202178/tpt-inheritance-with-non-abstract-base-class-in-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!