EF Inheritance and Foreign Keys

≡放荡痞女 提交于 2019-12-07 22:48:19

问题


I'm going for TPH inheritance here - one Comments table with a discriminator.

I keep getting the error:

The foreign key component 'ResponseId' is not a declared property on type 'PostComment'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property.

Whenever I try to perform a fresh migration.

Models:

public class Comment : Message {
    [Key]
    [Required]
    public int CommentId { get; set; }
    [Required]
    public int ResponseId { get; set; }
}

public class PostComment : Comment {
    public virtual PostResponse PostResponse { get; set; }
}
public class FlagComment : Comment {
    public virtual FlagResponse FlagResponse { get; set; }
}

Mappings:

public class CommentConfiguration : EntityTypeConfiguration<Comment> {
    public CommentConfiguration() {
        // Comment has Poster (From Message base class)
        HasRequired(c => c.Poster)
        .WithMany()
        .HasForeignKey(u => u.PosterId);
    }
}
public class PostCommentConfiguration : EntityTypeConfiguration<PostComment> {
    public PostCommentConfiguration() {
        // Comment has Response
        HasRequired(c => c.PostResponse)
        .WithMany(s => s.PostComments)
        .HasForeignKey(u => u.ResponseId);
    }
}
public class FlagCommentConfiguration : EntityTypeConfiguration<FlagComment> {
    public FlagCommentConfiguration() {
        // Comment has Response
        HasRequired(c => c.FlagResponse)
        .WithMany(s => s.FlagComments)
        .HasForeignKey(u => u.ResponseId);
    }
}

I even tried adding a new ResponseId to the derived Comment classes just to see if I could budge this forward:

    [Required]
    public new int ResponseId { get; set; }

To no avail - I get the same error. Why is this not working?

来源:https://stackoverflow.com/questions/20446856/ef-inheritance-and-foreign-keys

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