简体   繁体   中英

Entity framework - table per type issue

I'm having troubles using Entity Framework with table-per-type inheritance. The result I get is one extra column in the ' PostComments ' table.

Here's my code so far:

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; }
    }

PostComment class

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

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

PageComment class

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

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

My Seed() method

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 result: See a Page_Id column in PostComments table. That column should not be present there.

SQL结果

Can anyone tell me where I'm wrong? Any help is appreciated!

Your TPT definition is correct. This extra column that you see does not seem to be an issue with your TPT configuration. It looks like the Page class has a reference to PostComments which is creating this extra column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM