4生成协会FK关系列具有唯一索引

4生成协会FK关系列具有唯一索引

本文介绍了为什么不EF 4生成协会FK关系列具有唯一索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这样一个场景:实体框架4.0不会产生关联由表支持的实体具有的唯一的指数,我想知道这是为什么。

I have run into a scenario where Entity Framework 4.0 does not generate an association to an entity backed by a table having a unique index, and I am wondering why.

基本设置是这样的:比方说,我在SQL Server中的两个表2008 R2和一个外键关系:

The basic setup is this: Let's say I have two tables in SQL Server 2008 R2 and a foreign key relation:

CREATE TABLE [dbo].[User](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [GroupId] [int] NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[Group](
[Id] [int] IDENTITY(1,1) NOT NULL,
 CONSTRAINT [PK_Group] PRIMARY KEY CLUSTERED
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
    ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

ALTER TABLE [dbo].[User]  WITH CHECK ADD  CONSTRAINT [FK_User_Group]
    FOREIGN KEY([GroupId])
REFERENCES [dbo].[Group] ([Id])

此外,假设以下指标present:

Moreover, assume the following index is present:

CREATE NONCLUSTERED INDEX [IX_Group] ON [dbo].[Group]
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

如果我告诉设计师在Visual Studio 2010中生成一个ADO.NET实体数据模型,我得到了两个班的模式,用户集团用户有一个导航属性​​名为集团。这是所有罚款和幸福。

If I tell the designer in Visual Studio 2010 to generate an ADO.NET Entity Data Model I get a model with two classes, User and Group, User having a Navigation Property called Group. That is all fine and well.

现在,让我们说不是,该指数是这样的:

Now, let's say instead that the index looked like this:

CREATE UNIQUE NONCLUSTERED INDEX [IX_Group] ON [dbo].[Group]
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]

也就是说,的只有的事情,我所做的是使索引唯一索引。这样做之后,当我告诉Visual Studio的设计师生成实体模型,用户和组之间的关联并没有显示出来,并在用户有没有导航性能。检查生成EDMX文件显示,存储模型没有AssociationSet可言。

That is, the only thing I have done is make the index a unique index. Having done this, when I tell Visual Studio's designer to generate an Entity Model, the association between users and groups doesn't show up and the User has no navigation properties. Inspecting the generated EDMX file reveals that the storage model has no AssociationSet at all.

谁能解释这是为什么?为什么从造型的关系,唯一索引prevent EF?

Can anyone explain why this is? Why does the unique index prevent EF from modeling the relationship?

感谢你。

推荐答案

一个唯一索引允许1 NULL值,主键不允许空值。您将如何匹配空没事的时候等于NULL甚至没有另一个NULL

A unique index allows for 1 NULL value, a primary key doesn't allow NULLS. How will you match the NULL when nothing is equal to NULL not even another NULL

这篇关于为什么不EF 4生成协会FK关系列具有唯一索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 23:50