ASP.NET标识-如何更改dbo.AspNetUsers.Id转换为非聚集索引

本文关键字:转换 Id 索引 聚集 AspNetUsers dbo 标识 NET 何更改 ASP | 更新日期: 2023-09-27 18:25:47

我使用的是ASPNET Identity 2.0,需要:

  • 添加一个名为ApplicationUserId[int]聚集索引的新字段
  • 更改dbo。从聚集索引到非聚集索引的AspNetUsers.Id nvarchar字段

在我的Models''IdentityModel.cs中,我添加了:

    public class ApplicationUser: IdentityUser
    {
            [Index(IsClustered = true, IsUnique = true)]
            [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
            public int ApplicationUserId { get; set; }
    }

当然,我知道这会出错,因为我知道Id当前是一个聚集索引。如何更改Id,使默认值不是集群索引?

基本上,我想要这样做的原因是我们的DBA不想使用nvarchar(128)的外键(这是AspNetUsers表中的GUID)。

或者我只是将其创建为:

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ApplicationUserId { get; set; }

然后在创建表后手动修复索引?

或者我唯一的选择是使用整数键而不是字符串?

我使用的是.NET 4.5,并安装了Identity 2.0示例项目

 Install-Package Microsoft.AspNet.Identity.Samples -Pre

提前感谢您的帮助!

ASP.NET标识-如何更改dbo.AspNetUsers.Id转换为非聚集索引

我在没有将GUID[nvarchar]转换为[int]的情况下使其工作

基本上我更新了Models''IdentityModel.cs文件

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationUserId { get; set; }

然后我手动更新了表(AspNetUserLogins、AspNetUserClaims和AspNetUser Roles正在引用AspNetUsers。Id,我仍然希望保持的完整性

begin tran
ALTER TABLE [dbo].[AspNetUserLogins] DROP CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId]
ALTER TABLE [dbo].[AspNetUserClaims] DROP CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId]
ALTER TABLE [dbo].[AspNetUserRoles] DROP CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId]
ALTER TABLE dbo.[AspNetUsers] DROP CONSTRAINT [PK_dbo.AspNetUsers]
--Uncomment this line in case you need to modify this table again
--ALTER TABLE dbo.[AspNetUsers] DROP CONSTRAINT [dbo.AspNetUsers_ApplicationUserId]
-- Add the constraints back, but now nonclustered
ALTER TABLE dbo.[AspNetUsers] add constraint [PK_dbo.AspNetUsers] primary key     nonclustered 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
-- Add the other constraints back
ALTER TABLE [dbo].[AspNetUserLogins]  WITH CHECK ADD CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetUserClaims]  WITH CHECK ADD  CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetUserRoles]  WITH CHECK ADD  CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[AspNetUsers] ([Id])
ON DELETE CASCADE
GO
-- Add NEW constraint with unique clustered
ALTER TABLE dbo.[AspNetUsers] add constraint [dbo.AspNetUsers_ApplicationUserId] unique clustered 
(
[ApplicationUserId]
)
commit tran

当您签出dbo的创建表脚本时。AspNetUsers,现在看起来是这样的:

CREATE TABLE [dbo].[AspNetUsers](
    [Id] [nvarchar](128) NOT NULL,
    [ApplicationUserId] [int] IDENTITY(1,1) NOT NULL,
    [Email] [nvarchar](256) NULL,
    [EmailConfirmed] [bit] NOT NULL,
    [PasswordHash] [nvarchar](max) NULL,
    [SecurityStamp] [nvarchar](max) NULL,
    [PhoneNumber] [nvarchar](max) NULL,
    [PhoneNumberConfirmed] [bit] NOT NULL,
    [TwoFactorEnabled] [bit] NOT NULL,
    [LockoutEndDateUtc] [datetime] NULL,
    [LockoutEnabled] [bit] NOT NULL,
    [AccessFailedCount] [int] NOT NULL,
    [UserName] [nvarchar](256) NOT NULL,
 CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY NONCLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
 CONSTRAINT [dbo.AspNetUsers_ApplicationUserId] UNIQUE CLUSTERED 
(
    [ApplicationUserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]