Entity framework - table 每个类型问题

Entity framework - table per type issue

我在使用 Entity Framework 和 table-per-type 继承时遇到问题。我得到的结果是在“PostComments”table.

中多了一列

到目前为止,这是我的代码:

BaseComment class:

   public abstract class BaseComment
   {
        [Key]
        public int Id { get; set; }

        [Required]
        [AllowHtml]
        public string Content { get; set; }

        [Required]
        public DateTime TimeCreated { get; set; }

        public int? ParentId { get; set; }

        public int UserId { get; set; }

        [Required]
        public CommentStatus CommentStatus { get; set; }

        [ForeignKey("ParentId")]
        public virtual BaseComment Parent { get; set; }

        [ForeignKey("UserId")]
        public virtual User User { get; set; }
    }

发表评论class

public class PostComment : BaseComment
{
    public int PostId { get; set; }

    [ForeignKey("PostId")]
    public virtual Post Post { get; set; }
}

页面评论class

public class PageComment : BaseComment
{
    public int PageId { get; set; }

    [ForeignKey("PageId")]
    public virtual Page Page { get; set; }
}

我的 Seed() 方法

protected override void Seed(DAL.Contexts.XPressDbContext context)
{
    context.PostComments.AddOrUpdate(new PostComment()
    {
        Content = "asdasd",
        PostId = 1,
        TimeCreated = DateTime.Now,
        UserId = 1,
        CommentStatus = CommentStatus.Approved
    });

    context.PageComments.AddOrUpdate(new PageComment()
    {
        Content = "About page comment",
        PageId = 17,
        TimeCreated = DateTime.Now,
        UserId = 1,
        CommentStatus = CommentStatus.Approved
    });
}

SQL 结果:参见 PostComments table 中的 Page_Id 列。该列不应出现在那里。

谁能告诉我哪里错了?感谢您的帮助!

您的 TPT 定义是正确的。您看到的这个额外的列似乎不是您的 TPT 配置的问题。页面 class 似乎引用了创建此额外列的 PostComments。