我要在这里进行TPH继承 - 一个带有鉴别器的评论表。
我一直收到错误:
外键组件'ResponseId'不是声明的属性 输入'PostComment'。验证是否未明确排除它 来自模型,它是一个有效的原始属性。
每当我尝试进行新的迁移时。
模特:
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; }
}
映射:
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);
}
}
我甚至尝试在派生的Comment类中添加一个新的ResponseId,看看我是否可以让这个向前推进:
[Required]
public new int ResponseId { get; set; }
无济于事 - 我得到同样的错误。为什么这不起作用?