Disable associations when a foreign key do not exi

2019-08-30 18:34发布

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条回答
Bombasti
2楼-- · 2019-08-30 19:31
  • 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.

查看更多
登录 后发表回答