How to add a column to a link table with code first

大憨熊 提交于 2019-12-04 17:52:19
Slauma

It's not possible. The many-to-many join table is managed internally by Entity Framework and it must only contain the two key columns. You cannot customize this table.

What you need to do is:

  • Introduce a new entity into your model which represents the data in the join table (public class DatabaseSubject or something). You can customize this entity now as you like, for example add an IsPrimary property.
  • Remove the many-to-many mapping
  • Instead create two one-to-many relationships between DatabaseSubject and ResearchDatabase (one ResearchDatabase has many DatabaseSubjects) and between DatabaseSubject and Subject (one Subject has many DatabaseSubjects).

A more detailed description of such a scenario is here: Create code first, many to many, with additional fields in association table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!