Database first Entity Framework mapping unique foreign keys as one to many

江枫思渺然 提交于 2019-12-23 10:57:02

问题


I have a table in Microsoft SQL Server 2008 R2 called Page with a primary key called ID. I have another table called Navigation with a column PageID. PageID is a unique foreign key reference to the ID column of Page. This creates a one to one relationship between Navigation and Page records.

When generating models from the database, it creates a one to many relationship where a Page contains a list of Navigation records.

Is this simply the Entity Framework detecting that there is a foreign key involved and ignoring the uniqueness of the columns in the database?

The SQL for the PageID column in Navigation is:

[PageID] INTEGER FOREIGN KEY REFERENCES [Page](ID) UNIQUE NOT NULL

The SQL for the ID column in Page is:

[ID] INTEGER PRIMARY KEY IDENTITY(0, 1) NOT NULL

Here is the solution I had originally, which is what Ladislav was mentioning.

The SQL for the PageID column in Navigation was:

[ID] INTEGER PRIMARY KEY FOREIGN KEY REFERENCES [Page](ID) NOT NULL

回答1:


Entity framework doesn't support unique keys yet so this information is really ignored and one to many relation is mapped. The only way to use one to one relation in EF is through shared primary key (Navigation's ID will be FK to Page's ID).



来源:https://stackoverflow.com/questions/10061152/database-first-entity-framework-mapping-unique-foreign-keys-as-one-to-many

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