数据库第一实体框架映射独特的外键一对多

数据库第一实体框架映射独特的外键一对多

本文介绍了数据库第一实体框架映射独特的外键一对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在微软的SQL Server 2008 R2中的表名为与所谓的主键 ID 。我有另一个表名为导航与列 PAGEID PAGEID 是一个独特的外键参照 ID 页列。这将创建一个导航记录之间的一个关系。

在从数据库中生成模式,它会创建一个一对多的关系,其中一个包含导航记录。

这仅仅是一个实体框架检测到存在涉及外键,忽略列的唯一性数据库?

导航的SQL的 PAGEID 列是:

  [PAGEID] INTEGER外键参考文献:[首页](ID)UNIQUE NOT NULL
 

的SQL的 ID 列是:

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

下面是解决方案,我原本,这是拉吉斯拉夫被提。

的SQL的 PAGEID 栏目导航是:

  [ID]整数主键的外键参考文献:[首页](ID)NOT NULL
 

解决方案

实体框架不支持独特的键又那么真的是忽略了这一信息,并一对多的关系映射。在EF使用一对一关系的唯一方法是通过共享主键(导航的ID将FK页的ID)。

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
解决方案

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

这篇关于数据库第一实体框架映射独特的外键一对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 03:19