Wednesday, 3 June 2015

Asp.NET Identity SQL schema script

So this time I'm working on a project involving EntityFramework and ASP.NET Identity membership system. It is ok once you use Code First strategy rather than Database First to EntityFramework to generate your schema for the ASP.Identity. I'm using Database First and had problems with generating the required schema on-fly so I decided to post here SQL ASP.NET Identity's schema creation script to be run on your database just in case.


 /****** DATABASE ******/  
 USE [YourDatabase]  
   
 SET ANSI_NULLS ON  
   
 SET QUOTED_IDENTIFIER ON  
   
 /****** USER ******/  
   
 CREATE TABLE [dbo].[AspNetUsers](  
     [Id] [nvarchar](128) 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 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] TEXTIMAGE_ON [PRIMARY]  
   
   
 CREATE TABLE [dbo].[AspNetRoles](  
     [Id] [nvarchar](128) NOT NULL,  
     [Name] [nvarchar](256) NOT NULL,  
  CONSTRAINT [PK_dbo.AspNetRoles] 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]  
   
 /****** ROLE ******/  
   
 CREATE TABLE [dbo].[AspNetUserRoles](  
     [UserId] [nvarchar](128) NOT NULL,  
     [RoleId] [nvarchar](128) NOT NULL,  
  CONSTRAINT [PK_dbo.AspNetUserRoles] PRIMARY KEY CLUSTERED   
 (  
     [UserId] ASC,  
     [RoleId] ASC  
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
 ) ON [PRIMARY]  
   
 /****** USER - ROLES ******/  
   
 ALTER TABLE [dbo].[AspNetUserRoles] WITH CHECK ADD CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId] FOREIGN KEY([RoleId])  
 REFERENCES [dbo].[AspNetRoles] ([Id])  
 ON DELETE CASCADE  
   
 ALTER TABLE [dbo].[AspNetUserRoles] CHECK CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId]  
   
 ALTER TABLE [dbo].[AspNetUserRoles] WITH CHECK ADD CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId] FOREIGN KEY([UserId])  
 REFERENCES [dbo].[AspNetUsers] ([Id])  
 ON DELETE CASCADE  
   
 ALTER TABLE [dbo].[AspNetUserRoles] CHECK CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId]  
   
 /****** USER - LOGINS ******/  
   
 CREATE TABLE [dbo].[AspNetUserLogins](  
     [LoginProvider] [nvarchar](128) NOT NULL,  
     [ProviderKey] [nvarchar](128) NOT NULL,  
     [UserId] [nvarchar](128) NOT NULL,  
  CONSTRAINT [PK_dbo.AspNetUserLogins] PRIMARY KEY CLUSTERED   
 (  
     [LoginProvider] ASC,  
     [ProviderKey] ASC,  
     [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]  
   
 ALTER TABLE [dbo].[AspNetUserLogins] WITH CHECK ADD CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY([UserId])  
 REFERENCES [dbo].[AspNetUsers] ([Id])  
 ON DELETE CASCADE  
   
 ALTER TABLE [dbo].[AspNetUserLogins] CHECK CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId]  
   
 /****** USER - CLAIMS ******/  
   
 CREATE TABLE [dbo].[AspNetUserClaims](  
     [Id] [int] IDENTITY(1,1) NOT NULL,  
     [UserId] [nvarchar](128) NOT NULL,  
     [ClaimType] [nvarchar](max) NULL,  
     [ClaimValue] [nvarchar](max) NULL,  
  CONSTRAINT [PK_dbo.AspNetUserClaims] 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] TEXTIMAGE_ON [PRIMARY]  
   
 ALTER TABLE [dbo].[AspNetUserClaims] WITH CHECK ADD CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId] FOREIGN KEY([UserId])  
 REFERENCES [dbo].[AspNetUsers] ([Id])  
 ON DELETE CASCADE  
   
 ALTER TABLE [dbo].[AspNetUserClaims] CHECK CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId]  

No comments:

Post a Comment