本文介绍了当外键不存在时禁用关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为我的数据库创建了一个edmx。在其中Entity框架删除了一个表,而是在两个表之间创建了一个关联,因为它将列名与另一个表中的主键相匹配。



我不想要因为这些表之间没有真正的关联。例如:



> SomeTable




  • Id int pk



MiddleTable




  • SomeTableId int fk

  • SomeCode int



其他表




  • SomeCode int pk



这是 c c> SomeTable 和 OtherTable 之间的许多由两个集合属性建模:



SomeTable.OtherTables

OtherTable.SomeTables



中间表,连接表并不是真的必要。



对我来说有点令人惊讶,你说两个表之间没有关联,虽然显然在数据库中有外键。从技术上讲,它是一对多的关联。


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.

解决方案

  • 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.

这篇关于当外键不存在时禁用关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 01:28