I'm using ASPNET Identity 2.0 and need to:
- Add a new field called ApplicationUserId [int] clustered index
- Change the dbo.AspNetUsers.Id nvarchar field from clustered to a Nonclustered index
In my Models\IdentityModel.cs, I added:
public class ApplicationUser: IdentityUser
{
[Index(IsClustered = true, IsUnique = true)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationUserId { get; set; }
}
Of course, I knew this would error since I know that Id is currently a clustered index.
How do I change the Id so that the default is not a clusered index?
Basically the reason I want to this is that our DBA does not want to use foreign keys that's a nvarchar(128) (which is this GUID from AspNetUsers table).
Or do I just create it as:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationUserId { get; set; }
and then manually fix the indexes after the tables are created?
Or is my only option to use integer keys instead of strings?
I'm using .NET 4.5 and installed the Identity 2.0 Sample Project
Install-Package Microsoft.AspNet.Identity.Samples -Pre
Thanks in advance for your help!
I got it to work without converting the GUID [nvarchar] to an [int]
Basically I updated the Models\IdentityModel.cs file
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ApplicationUserId { get; set; }
Then I manually updated the tables (AspNetUserLogins, AspNetUserClaims, and AspNetUserRoles are referencing the AspNetUsers.Id which i still want to keep intact
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
When you check out the create table script for dbo.AspNetUsers, it will now look like this:
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]