How to create ASP.Net Identity tables inside existing database?

后端 未结 4 378
一向
一向 2020-12-13 20:03

I am building my first MVC 5 / Entity Framework application. I used the database first method to pull in my data from an existing SQL server. The existing SQL database rec

4条回答
  •  时光取名叫无心
    2020-12-13 20:27

    I did a sequence of steps:

    Step 1. Create a brand-new (and temporary) project use the template what likes your existing project. When creating new project, you must choose option has Identity, and Individual...

    Step 2. You already have a project. Add Scaffolding items... Choose Identity...

    Step 3. Open Package Manager Console, run command

    Update-Database
    

    you will see at console like this (not exactly like yours, because I use new version of Visual Studio, .NET Core framework, posted time, etc.)

    Each package is licensed to you by its owner. NuGet is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.
    
    Package Manager Console Host Version 5.5.0.6293
    
    Type 'get-help NuGet' to see all available NuGet commands.
    
    PM> Update-Database
    Build started...
    Build succeeded.
    Microsoft.EntityFrameworkCore.Infrastructure[10403]
          Entity Framework Core 3.1.1 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (15ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT 1
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (13ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT 1
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [__EFMigrationsHistory] (
              [MigrationId] nvarchar(150) NOT NULL,
              [ProductVersion] nvarchar(32) NOT NULL,
              CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT 1
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT [MigrationId], [ProductVersion]
          FROM [__EFMigrationsHistory]
          ORDER BY [MigrationId];
    Microsoft.EntityFrameworkCore.Migrations[20402]
          Applying migration '00000000000000_CreateIdentitySchema'.
    Applying migration '00000000000000_CreateIdentitySchema'.
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetRoles] (
              [Id] nvarchar(450) NOT NULL,
              [Name] nvarchar(256) NULL,
              [NormalizedName] nvarchar(256) NULL,
              [ConcurrencyStamp] nvarchar(max) NULL,
              CONSTRAINT [PK_AspNetRoles] PRIMARY KEY ([Id])
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetUsers] (
              [Id] nvarchar(450) NOT NULL,
              [UserName] nvarchar(256) NULL,
              [NormalizedUserName] nvarchar(256) NULL,
              [Email] nvarchar(256) NULL,
              [NormalizedEmail] nvarchar(256) NULL,
              [EmailConfirmed] bit NOT NULL,
              [PasswordHash] nvarchar(max) NULL,
              [SecurityStamp] nvarchar(max) NULL,
              [ConcurrencyStamp] nvarchar(max) NULL,
              [PhoneNumber] nvarchar(max) NULL,
              [PhoneNumberConfirmed] bit NOT NULL,
              [TwoFactorEnabled] bit NOT NULL,
              [LockoutEnd] datetimeoffset NULL,
              [LockoutEnabled] bit NOT NULL,
              [AccessFailedCount] int NOT NULL,
              CONSTRAINT [PK_AspNetUsers] PRIMARY KEY ([Id])
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetRoleClaims] (
              [Id] int NOT NULL IDENTITY,
              [RoleId] nvarchar(450) NOT NULL,
              [ClaimType] nvarchar(max) NULL,
              [ClaimValue] nvarchar(max) NULL,
              CONSTRAINT [PK_AspNetRoleClaims] PRIMARY KEY ([Id]),
              CONSTRAINT [FK_AspNetRoleClaims_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetUserClaims] (
              [Id] int NOT NULL IDENTITY,
              [UserId] nvarchar(450) NOT NULL,
              [ClaimType] nvarchar(max) NULL,
              [ClaimValue] nvarchar(max) NULL,
              CONSTRAINT [PK_AspNetUserClaims] PRIMARY KEY ([Id]),
              CONSTRAINT [FK_AspNetUserClaims_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetUserLogins] (
              [LoginProvider] nvarchar(128) NOT NULL,
              [ProviderKey] nvarchar(128) NOT NULL,
              [ProviderDisplayName] nvarchar(max) NULL,
              [UserId] nvarchar(450) NOT NULL,
              CONSTRAINT [PK_AspNetUserLogins] PRIMARY KEY ([LoginProvider], [ProviderKey]),
              CONSTRAINT [FK_AspNetUserLogins_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetUserRoles] (
              [UserId] nvarchar(450) NOT NULL,
              [RoleId] nvarchar(450) NOT NULL,
              CONSTRAINT [PK_AspNetUserRoles] PRIMARY KEY ([UserId], [RoleId]),
              CONSTRAINT [FK_AspNetUserRoles_AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [AspNetRoles] ([Id]) ON DELETE CASCADE,
              CONSTRAINT [FK_AspNetUserRoles_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (2ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE TABLE [AspNetUserTokens] (
              [UserId] nvarchar(450) NOT NULL,
              [LoginProvider] nvarchar(128) NOT NULL,
              [Name] nvarchar(128) NOT NULL,
              [Value] nvarchar(max) NULL,
              CONSTRAINT [PK_AspNetUserTokens] PRIMARY KEY ([UserId], [LoginProvider], [Name]),
              CONSTRAINT [FK_AspNetUserTokens_AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [AspNetUsers] ([Id]) ON DELETE CASCADE
          );
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE INDEX [IX_AspNetRoleClaims_RoleId] ON [AspNetRoleClaims] ([RoleId]);
    infoverbose: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    : Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE UNIQUE INDEX [RoleNameIndex] ON [AspNetRoles] ([NormalizedName]) WHERE [NormalizedName] IS NOT NULL;
    infoverbose: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    : Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE INDEX [IX_AspNetUserClaims_UserId] ON [AspNetUserClaims] ([UserId]);
    infoverbose: Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    : Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE INDEX [IX_AspNetUserLogins_UserId] ON [AspNetUserLogins] ([UserId]);
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE INDEX [IX_AspNetUserRoles_RoleId] ON [AspNetUserRoles] ([RoleId]);
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE INDEX [EmailIndex] ON [AspNetUsers] ([NormalizedEmail]);
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          CREATE UNIQUE INDEX [UserNameIndex] ON [AspNetUsers] ([NormalizedUserName]) WHERE [NormalizedUserName] IS NOT NULL;
    Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
          VALUES (N'00000000000000_CreateIdentitySchema', N'3.1.1');
    Done.
    PM> 
    

    These SQL scripts are what you need.

    Screenshot is merely for idea illustration

提交回复
热议问题