我正在尝试使用存储过程填充 AspNetUsers 表,但出现以下错误:
Msg 515,级别 16,状态 2,过程 Insert_Users,第 36 行无法 将值 NULL 插入到"Id"列中,表 'MyDatabase.dbo.AspNetUsers';列不允许空值。插入 失败。
我在其他一些帖子上读到,Id 列上应该有以下属性:[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
,但我无法找到物理声明的位置。
我不想手动插入所有用户,因为我没有时间了,我需要尽快完成。
BEGIN
SET NOCOUNT ON;
DECLARE @id uniqueidentifier
DECLARE @email nvarchar(256)
DECLARE @emailconfirmed bit
SET @emailconfirmed = 0
DECLARE @twofactorenabled bit
SET @twofactorenabled = 0
DECLARE @lockoutenabled bit
SET @lockoutenabled = 1
DECLARE @accessFailed bit
SET @accessFailed = 1
DECLARE @username nvarchar(256)
DECLARE @fname nvarchar(50)
DECLARE @lname nvarchar(50)
DECLARE @athleteKey int
SET @athleteKey = 41809
DECLARE @atheletsCount int
SET @atheletsCount = 0;
SET @atheletsCount = (SELECT COUNT(*) FROM [BIBD].[dbo].[Athlete])
WHILE @athleteKey < @atheletsCount
SET @id = NEWID() --Line 36
print CAST(@id AS nvarchar(128))
SET @fname = (SELECT FirstName FROM [BIBD].[dbo].[Athlete] where AthleteKey=@athleteKey)
SET @lname = (SELECT LastName FROM [BIBD].[dbo].[Athlete] where AthleteKey=@athleteKey)
SET @username = CONCAT(LOWER(@fname),'.',LOWER(@lname))
SET @email = CONCAT(LOWER(@fname), '.', LOWER(@lname), '@gmail.com')
INSERT INTO [MyDatabase].[dbo].[AspNetUsers]
(Id
,Email
,EmailConfirmed
,[TwoFactorEnabled]
,LockoutEnabled
,AccessFailedCount
,UserName
,FirstName
,LastName)
VALUES
(CAST(@id AS nvarchar(128))
,@email
,@emailconfirmed
,@twofactorenabled
,@lockoutenabled
,@accessFailed
,@username
,@fname
,@lname)
IF @athleteKey % 5 = 0
INSERT INTO [MyDatabase].[dbo].[AspNetUserRoles]
(UserId,RoleId) VALUES (@id, 3)
ELSE
INSERT INTO [MyDatabase].[dbo].[AspNetUserRoles]
(UserId,RoleId) VALUES (@id, 4)
SET @athleteKey = @athleteKey+1
END
你需要在
WHILE 循环中开始-结束。
否则,它只是不为@id赋值,也不处理循环