Disable associations when a foreign key do not exist

只愿长相守 提交于 2019-12-25 16:54:42

问题


I've created an edmx for my database. In it Entity framework removed a table and instead created an association between two tables because it matches a column name with the primary key in the other table.

I do not want that as there is no real association between those tables. How can I remove that association and get a class for the middle table instead?

Example:

SomeTable

  • Id int pk

MiddleTable

  • SomeTableId int fk
  • SomeCode int

OtherTable

  • SomeCode int pk

It's the MiddleTable which do not get a class.


回答1:


  • Remove one table from the edmx, e.g. OtherTable.
  • Update model from database and add MiddleTable.
  • Update model from database and add OtherTable.

When I do this with a similar model I end up with an association between SomeTable and MiddleTable and an unassociated OtherTable. Now you can add/remove associations manually as you wish.

It's normal EF behavior not to create a class for the middle table. This is a so-called many to many association between SomeTable and OtherTable which can be modelled by two collection properties:

SomeTable.OtherTables
OtherTable.SomeTables

The middle table, the junction table, is not really necessary.

It's a bit surprising to me that you say that there is no association between the two tables although, apparently, in the database there are foreign keys. Technically, it is a many to many association.



来源:https://stackoverflow.com/questions/22017455/disable-associations-when-a-foreign-key-do-not-exist

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