我试图使用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作者可以链接到其他BlogPosts并按相关性顺序分配和排序 .
我遇到的问题是在Entity Framework中映射这些问题 . 这是我试过的......
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'无效 . 指定的列数必须与主键列数相匹配.
任何建议/方向将不胜感激 .