数据库第一个实体框架将唯一的外键映射为多个。

时间:2021-08-24 02:16:27

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.

我在Microsoft SQL Server 2008 R2中有一个名为Page的表,它的主键是ID。PageID是对页面ID列的唯一外键引用。这为导航和页面记录之间的关系创建了一个一对一的关系。

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列的SQL是:

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

The SQL for the ID column in Page is:

页面中ID列的SQL为:

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

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

这是我最初的解决方案,这是Ladislav提到的。

The SQL for the PageID column in Navigation was:

导航中PageID列的SQL是:

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

1 个解决方案

#1


6  

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

实体框架不支持唯一的键,所以这个信息被忽略了,并且映射了一个到多个关系。在EF中使用一对一关系的唯一方法是通过共享主键(导航的ID将是FK到页面的ID)。

#1


6  

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

实体框架不支持唯一的键,所以这个信息被忽略了,并且映射了一个到多个关系。在EF中使用一对一关系的唯一方法是通过共享主键(导航的ID将是FK到页面的ID)。