以下是asp成员资格提供者的表。我正在尝试添加同一应用程序的多个用户,但我无法在ApplicationId
列中添加重复值。为什么会这样?
这是我用于在表中插入记录的查询:
Insert into Users
values ('97C84F32-26A6-4A6C-960E-89A6A14D54E4', 'C2CADEDE-DA70-4C25-B717-6970344C8B34', 'John_doe', '', '', 1, '2014-11-05 21:28:33.043')
Insert into Users
values ('97C84F32-26A6-4A6C-960E-89A6A14D54E4', 'D2CADEDE-CA70-4C25-B717-6970344C8B34', 'John_doe1', '', '', 1, '2014-11-05 21:28:33.043')
错误
无法在对象' dbo.Users'中插入重复的键行。具有唯一索引&#asp ;_Users_Index'。重复键值为(97c84f32-26a6-4a6c-960e-89a6a14d54e4,)。
这是我的用户表查询:
CREATE TABLE [dbo].[Users]
(
[ApplicationId] [uniqueidentifier] NOT NULL,
[UserId] [uniqueidentifier] NOT NULL,
[UserName] [nvarchar](256) NOT NULL,
[LoweredUserName] [nvarchar](256) NOT NULL,
[MobileAlias] [nvarchar](16) NULL,
[IsAnonymous] [bit] NOT NULL,
[LastActivityDate] [datetime] NOT NULL,
PRIMARY KEY NONCLUSTERED ([UserId] 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].[Users] ADD DEFAULT (newid()) FOR [UserId]
GO
ALTER TABLE [dbo].[Users] ADD DEFAULT (NULL) FOR [MobileAlias]
GO
ALTER TABLE [dbo].[Users] ADD DEFAULT ((0)) FOR [IsAnonymous]
GO
ALTER TABLE [dbo].[Users] WITH CHECK
ADD FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[Applications] ([ApplicationId])
GO
任何帮助都会非常感激
答案 0 :(得分:1)
您收到的错误是因为PRIMARY KEY
列上的表格中有UserId
。这意味着您无法在该列中插入具有相同值的多个行。
如果要插入相同的UserId
但ApplicationId
不同,则应将主键更改为包含UserId
和ApplicationId
的复合键。< / p>
或者,可能是您的INSERT
语句需要更改,因为您依赖于列位置正确。而是指定列:
Insert into Users
(ApplicationId, UserId, UserName, LoweredUserName, MobileAlias, IsAnonymous, LastActivityDate)
values
('97C84F32-26A6-4A6C-960E-89A6A14D54E4','C2CADEDE-DA70-4C25-B717-6970344C8B34','John_doe','','',1,'2014-11-05 21:28:33.043')
Insert into Users
(ApplicationId, UserId, UserName, LoweredUserName, MobileAlias, IsAnonymous, LastActivityDate)
values
('97C84F32-26A6-4A6C-960E-89A6A14D54E4','D2CADEDE-CA70-4C25-B717-6970344C8B34','John_doe1','','',1,'2014-11-05 21:28:33.043')
<强>更新强>
通过一些研究,默认情况下aspnet_Users
表在UserId
和LoweredUserName
上有一个独特的索引。只需指定一个唯一的LoweredUserName值:
Insert into Users
(ApplicationId, UserId, UserName, LoweredUserName, MobileAlias, IsAnonymous, LastActivityDate)
values
('97C84F32-26A6-4A6C-960E-89A6A14D54E4','C2CADEDE-DA70-4C25-B717-6970344C8B34','John_doe','john doe','',1,'2014-11-05 21:28:33.043')
Insert into Users
(ApplicationId, UserId, UserName, LoweredUserName, MobileAlias, IsAnonymous, LastActivityDate)
values
('97C84F32-26A6-4A6C-960E-89A6A14D54E4','D2CADEDE-CA70-4C25-B717-6970344C8B34','John_doe1','john_doe1','',1,'2014-11-05 21:28:33.043')