LinkTable 上的两个一对多关联
我有一个名为:BlogsBlogPosts 的 LinkTable,它将另外两个名为:Blogs 和 BlogPosts 的表关联在一起,我希望链接表出现在实体列表中,并且链接表上定义了两个一对多关系,我定义它是否正确像这样?:
public class BlogsBlogPostConfiguration : EntityTypeConfiguration<BlogsBlogPost>
{
public BlogsBlogPostConfiguration()
{
this.HasRequired(bb => bb.Blog)
.WithMany(b => b.BlogsBlogPosts)
.HasForeignKey(bb =>bb.BlogID);
this.HasRequired(bb => bb.BlogPost)
.WithMany(bp => bp.BlogsBlogPosts)
.HasForeignKey(bb => bb.BlogPostID);
}
}
I have a LinkTable named: BlogsBlogPosts that relates two other tables named: Blogs and BlogPosts together, I want the link table to appear in the list of entities and There will be two one to many relationships defined on the linktable am I correct to define it like this?:
public class BlogsBlogPostConfiguration : EntityTypeConfiguration<BlogsBlogPost>
{
public BlogsBlogPostConfiguration()
{
this.HasRequired(bb => bb.Blog)
.WithMany(b => b.BlogsBlogPosts)
.HasForeignKey(bb =>bb.BlogID);
this.HasRequired(bb => bb.BlogPost)
.WithMany(bp => bp.BlogsBlogPosts)
.HasForeignKey(bb => bb.BlogPostID);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。对我来说唯一剩下的问题是:
BlogsBlogPost
的主键属性怎么样?两个选项:您要么拥有一个独特的关键属性,例如
public int BlogsBlogPostID { get;放; }
或public int ID { get;放; }
在此实体中。在这种情况下,(BlogID
、BlogPostID
) 的组合不必是唯一的。或者您有一个由 (
BlogID
,BlogPostID
) 组成的复合键。您必须使用[Key]
注释来标记它或在 Fluent API 中定义它:现在 (
BlogID
,BlogPostID
) 的组合必须是唯一的。这更像是“与链接表中附加数据的多对多关系”类型的关系。两种模型都是可能的。使用哪个取决于您的业务需求。
Yes. The only question which remains for me is: What about the primary key property of
BlogsBlogPost
? Two options:You either have a distinct key property like
public int BlogsBlogPostID { get; set; }
orpublic int ID { get; set; }
in this entity. In this case combinations of (BlogID
,BlogPostID
) don't have to be unique.Or you have a composite key consisting of (
BlogID
,BlogPostID
). You would have to mark this with the[Key]
annotation or define it in Fluent API:Now combinations of (
BlogID
,BlogPostID
) must be unique. This is more of a "many-to-many relationship with additional data in the link table" type of relationship.Both models are possible. Which to use depends on your business needs.