实体框架中与联结表的多对多关系?
我试图根据以下帖子在实体框架中创建多对多关系(代码优先): MVC 和实体框架中有限数量的选择的数据库设计?
但是,我无法让它正常工作,我我确信我正在以错误的方式做一些非常简单的事情。这是我尝试中没有得到的图表:
联结表的要点是我需要有关系中的一个额外属性,级别,所以我不能只选择顾问和项目之间的直接关系。我在设计器中手动添加了 ConsultantProgramLink 实体,然后分别添加了 Program 和 Consultant 的关联,选择为每个添加 FK,然后将它们设为主键。但是当我这样做时,它并没有像我预期的那样工作:
如果我在顾问和程序之间建立了直接关联,我就可以在我的代码中引用顾问.程序。但现在这不适用于联结表。有什么方法可以解决这个问题,还是我总是必须通过连接属性(Consultant.ConsultantProgramLink.Programs)?无论如何,即使我确实尝试通过交界处属性,它也无济于事。我可以在代码中执行 Consultant.ConsultantProgramLink,但另一个点没有给我导航属性 Programs(由于某种原因,它也变成了简单的 Program,为什么?如果我最终能够访问它们,我可以重命名它们吗?) 。
那么我做错了什么?为什么我无法在代码中通过点表示法访问属性?
I'm trying to create a many-to-many relationship in Entity Framework (code first), according to the following post: Database design for limited number of choices in MVC and Entity Framework?
However, I can't get it to work properly, and I'm sure I'm doing something very simple the wrong way. Here's the diagram I have no from my attempts:
The point of the junction table is that I need to have an extra property, Level, in the relationship, so I can't just go with a direct relationship between Consultant and Program. I added the ConsultantProgramLink entity manually in the designer, and then added associations to Program and Consultant respectively, selecting to add a FK for each, and then made them both primary keys. But when I do it like this it doesn't work as I expected:
If I had done a direct association between Consultant and Program, I would have been able to refer to, say, Consultant.Programs in my code. But that doesn't work now with the junction table. Is there any way to remedy this, or do I always have to go through the junction property (Consultant.ConsultantProgramLink.Programs)? In any case, even if I do try to go through the junction property it doesn't help. I can do Consultant.ConsultantProgramLink in my code, but another dot doesn't give me the navigation property Programs (which for some reason also became simply Program, why? Can I just rename them if I eventually get access to them at all?).
So what am I doing wrong? Why can't I access the properties through dot notation in my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一旦将联结表建模为实体,您确实会失去
Consultant
和Program
之间的直接多对多关系。这就是它的工作原理。您将在联结表中拥有直接的多对多关系或附加属性。两者都不是。如果您想要两者,您可以尝试在Consultant
上创建自定义Programs
属性,并使用 linq 查询来获取相关程序:该示例也是对上一个问题的解释。您不能在
ConsultantProgramLink
上拥有Program
属性,因为它是相关实体的集合,而不是单个实体(应称为ConsultantProgramLinks
)。ConsultantProgramLink
实体中的属性简称为Program
,因为它代表单个实体而不是集合。编辑:
如果您需要每个
程序
自动与每个顾问
关联,则必须在创建新程序
时强制执行它。将联结表公开为单独的实体可能会让您轻松实现它:如果您添加新的顾问,您将必须以相同的方式创建到所有程序的链接。
缺点是,如果您有 1000 名顾问,此构造将创建 1001 个数据库插入,其中每个插入将在数据库的单独往返中执行。为了避免这种情况,唯一的选择是在程序表上使用存储过程或触发器。
Once you model a junction table as an entity you indeed lose direct many-to-many relation between
Consultant
andProgram
. That is how it works. You will either have direct many-to-many relation or additional properties in the junction table. Not both. If you want both you can try creating customPrograms
property onConsultant
and use linq query to get related programs:The example is also the explanation of your last problem. You can't have
Program
property onConsultantProgramLink
because it is a collection of related entities, not single entity (it should be calledConsultantProgramLinks
). The property inConsultantProgramLink
entity is called simplyProgram
because it represents single entity not collection.Edit:
If you need each
Program
to be automatically associated with eachConsultant
you must enforce it when you are going to create newProgram
. Having junction table exposed as separate entity will probably allow you achieving it easily:If you add new
Consultant
you will have to create links to all programs in the same way.The disadvantage is that if you have for example 1000 consultants this construct will create 1001 database inserts where each insert will be executed in separate roundtrip to the database. To avoid it the only option is either use stored procedur or trigger on Program table.