EF6 不从数据库生成外键关联
EF6 Does not generate foreign key association from database
问题:
我正在尝试使用数据库优先方法创建 EF6 模型。简单来说,我有2个tables tblUser
和tblMilkMan
,它们在UserID
列上有外键关系。但是当我创建一个新的 EDMX 图并添加 2 tables:
- 我没有看到关系
- 没有创建导航属性
请告诉我我做错了什么!
数据库模式:
Table 脚本:
CREATE TABLE [dbo].[tblMilkMan]
(
[RecordID] [INT] NOT NULL
IDENTITY(1, 1) ,
[UserID] [INT] NOT NULL ,
[IsMyTurn] [BIT] NOT NULL ,
[RoundRobinOrder] [INT] NOT NULL
)
ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblMilkMan] ADD CONSTRAINT [PK_tblMilkMan] PRIMARY KEY CLUSTERED ([RecordID]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblMilkMan] ADD CONSTRAINT [FK__MilkMan__User__UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[tblUser] ([UserID]) ON DELETE CASCADE
GO
CREATE TABLE [dbo].[tblUser]
(
[UserID] [INT] NOT NULL
IDENTITY(1, 1) ,
[LogonName] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[Client] [CHAR](4) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[Password] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[PasswordExpires] [DATETIME] NULL ,
[PasswordExpiresAfter] [INT] NOT NULL
CONSTRAINT [DF_tblUser_PasswordExpiresAfter] DEFAULT ( 9999 ) ,
[FirstName] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[LastName] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL
)
ON [PRIMARY]
WITH (
DATA_COMPRESSION = ROW)
GO
ALTER TABLE [dbo].[tblUser] ADD CONSTRAINT [PK_tblUser] PRIMARY KEY CLUSTERED ([UserID]) WITH (DATA_COMPRESSION = ROW) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_tblUser] ON [dbo].[tblUser] ([UserID]) WITH (FILLFACTOR=90, DATA_COMPRESSION = ROW) ON [PRIMARY]
GO
EDMX 设计器视图:
对我没有帮助的相关链接:
环境:
- Visual Studio 终极 2012
- SQL 服务器 2014
- EF6
- Web API2
问题出在下一行 -
CREATE UNIQUE NONCLUSTERED INDEX [IX_tblUser] ON [dbo].[tblUser] ([UserID])
WITH (FILLFACTOR=90, DATA_COMPRESSION = ROW) ON [PRIMARY]
GO
我不知道在同一列上有聚簇索引和非聚簇索引的原因。常见的最佳做法是创建一个主键作为聚簇索引。 (Source)
如果删除 NONCLUSTERED INDEX,它将在 EDMX 中看到关系。
如何删除?
问题:
我正在尝试使用数据库优先方法创建 EF6 模型。简单来说,我有2个tables tblUser
和tblMilkMan
,它们在UserID
列上有外键关系。但是当我创建一个新的 EDMX 图并添加 2 tables:
- 我没有看到关系
- 没有创建导航属性
请告诉我我做错了什么!
数据库模式:
Table 脚本:
CREATE TABLE [dbo].[tblMilkMan]
(
[RecordID] [INT] NOT NULL
IDENTITY(1, 1) ,
[UserID] [INT] NOT NULL ,
[IsMyTurn] [BIT] NOT NULL ,
[RoundRobinOrder] [INT] NOT NULL
)
ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblMilkMan] ADD CONSTRAINT [PK_tblMilkMan] PRIMARY KEY CLUSTERED ([RecordID]) ON [PRIMARY]
GO
ALTER TABLE [dbo].[tblMilkMan] ADD CONSTRAINT [FK__MilkMan__User__UserID] FOREIGN KEY ([UserID]) REFERENCES [dbo].[tblUser] ([UserID]) ON DELETE CASCADE
GO
CREATE TABLE [dbo].[tblUser]
(
[UserID] [INT] NOT NULL
IDENTITY(1, 1) ,
[LogonName] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[Client] [CHAR](4) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[Password] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[PasswordExpires] [DATETIME] NULL ,
[PasswordExpiresAfter] [INT] NOT NULL
CONSTRAINT [DF_tblUser_PasswordExpiresAfter] DEFAULT ( 9999 ) ,
[FirstName] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL ,
[LastName] [VARCHAR](20) COLLATE SQL_Latin1_General_CP437_CI_AS
NOT NULL
)
ON [PRIMARY]
WITH (
DATA_COMPRESSION = ROW)
GO
ALTER TABLE [dbo].[tblUser] ADD CONSTRAINT [PK_tblUser] PRIMARY KEY CLUSTERED ([UserID]) WITH (DATA_COMPRESSION = ROW) ON [PRIMARY]
GO
CREATE UNIQUE NONCLUSTERED INDEX [IX_tblUser] ON [dbo].[tblUser] ([UserID]) WITH (FILLFACTOR=90, DATA_COMPRESSION = ROW) ON [PRIMARY]
GO
EDMX 设计器视图:
对我没有帮助的相关链接:
环境:
- Visual Studio 终极 2012
- SQL 服务器 2014
- EF6
- Web API2
问题出在下一行 -
CREATE UNIQUE NONCLUSTERED INDEX [IX_tblUser] ON [dbo].[tblUser] ([UserID])
WITH (FILLFACTOR=90, DATA_COMPRESSION = ROW) ON [PRIMARY]
GO
我不知道在同一列上有聚簇索引和非聚簇索引的原因。常见的最佳做法是创建一个主键作为聚簇索引。 (Source)
如果删除 NONCLUSTERED INDEX,它将在 EDMX 中看到关系。