实体框架中的多对多关系,其中连接表具有两个以上字段?
我正在将数据库导入实体框架,但在处理如下所示的多对多关系时遇到了问题:
我的理解是,如果“连接表”(中间一个)仅包含两个字段(外键),则 EF会自动删除中间表并创建多对多关系。不幸的是,我无法控制数据库模式,所以有人知道是否有办法手动复制该行为?
根据记录,Employee_Employee_Type 中的 Id 字段背后没有任何用途,它只是设计得很糟糕。
I am importing a database into Entity Framework and I'm having trouble with a many-to-many relationship that looks like this:
My understanding is that if the "join table" (the middle one) contains only two fields (the foreign keys) then EF will automatically remove the middle table and create a many-to-many relationship. Unfortunately I don't have control over the database schema, so does anyone know if there's a way to replicate that behaviour manually?
For the record, there is no purpose behind that Id field in Employee_Employee_Type, it's just poorly designed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Entity Framework 4 食谱,第 554 页,这就是您的做法(它不太漂亮)。
本质上,您想要创建 Employee_Employee_type 映射表的视图,但没有额外的列,然后手动将其映射到其他两个表。以下是相关页面的图片。我认为(并且希望!)这受到学术免费使用版权法的保护……
顺便说一句,这本书太棒了,所以我建议购买它。希望如果作者碰巧遇到这种情况,我会更喜欢他。
According to Entity Framework 4 recipes, page 554, this is how you'd do it (it's not pretty).
Essentially, you want to create a View of the Employee_Employee_type mapping table, but without the extra column, then manually map it to the other two tables. Below are pics of the relevant pages. I think (and hope!) this is covered by academic free-use copyright laws...
The book is fantastic, BTW, so I'd recommend buying it. Hopefully that will endear me to the author if he happens upon this.
是的,这可能是为您提供更清晰的查询体验的最佳方法,并且对于插入和更新,您始终可以使用链接表。
最终,不会提及什么时候,EF 将支持带有负载的导航属性。
我很高兴人们喜欢这项工作。老实说,写这本书花了一年的时间。
yeah that is probably the best approach to give you cleaner experience on querying and for the inserting and updating you can always use the link table.
Eventually, will not mention when, EF will support navigation properties with payload.
I am glad people like the work. It honestly took 1 year to write the book.