更具体地说,我想将Email
表中的AspNetUsers
列设置为另一个表中的外键。
我希望能很好地解释我的问题。
答案 0 :(得分:2)
在SQL Server中,外键必须是引用表的主键。它不允许任何其他列(或复合列键),即使该列具有UNIQUE
约束。
所以不,你不能。
答案 1 :(得分:0)
是的,您可以使用流利的api在asp.net核心中通过主键进行外键操作。您可以从here
的Microsoft文档中看到一个示例答案 2 :(得分:0)
我能够为非pk列创建外键。没问题。
CREATE TABLE [dbo].[t1](
[id] [int] NOT NULL,
[fldKey] [int] NULL,
CONSTRAINT [PK_t1] 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]
GO
CREATE TABLE [dbo].[t2](
[id] [int] NOT NULL,
[t1] [int] NULL,
CONSTRAINT [PK_t2] 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]
GO
ALTER TABLE [dbo].[t2] WITH CHECK ADD CONSTRAINT [FK_t2_t1] FOREIGN
KEY([t1])
REFERENCES [dbo].[t1] ([fldKey])
GO
ALTER TABLE [dbo].[t2] CHECK CONSTRAINT [FK_t2_t1]
GO