我正在尝试使用 Entity Framework (版本6.1.3)为以下类建模
public class BlogPost
{
public Guid BlogPostId { get; set; }
public string Slug { get; set; }
public string Content { get; set; }
public DateTime Created { get; set; }
// Navigation
public virtual ICollection<BlogPostBlogPostLink> RelatedBlogs { get; set;
}
public class BlogPostBlogPostLink
{
public Guid BlogPostId { get; set; }
public Guid LinkedBlogPostId { get; set; }
public int Relevance { get; set; }
// Navigation
public virtual BlogPost BlogPost { get; set; }
public virtual BlogPost LinkedBlogPost { get; set; }
}
这个想法是BlogPost作者可以链接到其他BlogPost并按相关性顺序分配和排序。
我遇到的问题是在实体框架中映射这些问题。这就是我试过的......
public class BlogPostMap
: EntityTypeConfiguration<BlogPost>
{
public BlogPostMap()
{
ToTable("blog_BlogPost");
// Primary Key
HasKey(bp => bp.BlogPostId);
HasMany(bp => bp.RelatedBlogs)
.WithMany()
.Map(m =>
{
m.MapLeftKey("BlogPostId");
m.MapRightKey("LinkedBlogPostId");
m.ToTable("blog_BlogPostBlogPostLink");
});
}
}
public class BlogPostBlogPostLinkMap
: EntityTypeConfiguration<BlogPostBlogPostLink>
{
public BlogPostBlogPostLinkMap()
{
ToTable("blog_BlogPostBlogPostLink");
// Composite Primary Key
HasKey(bpl => new { bpl.BlogPostId, bpl.LinkedBlogPostId });
// Properties
Property(bp => bp.BlogPostId)
.IsRequired()
.HasColumnName("BlogPostId");
Property(bpl => bpl.LinkedBlogPostId)
.IsRequired()
.HasColumnName("LinkedBlogPostId");
// Relationships
HasRequired(bpl => bpl.BlogPost);
HasRequired(bpl => bpl.LinkedBlogPost);
}
}
当我运行代码时,出现以下错误:
_指定的关联外键列'LinkedBlogPostId'无效。指定的列数必须与主键列的数量匹配._
非常感谢任何建议/指示。