简体   繁体   English

将用户ASP.NET网页表迁移到MVC 6

[英]Migrate Users ASP.NET Web Pages tables to MVC 6

Info 信息
I want to 'upgrade' my ASP.NET Web Page project to an ASP.NET MVC project by just starting over, it are only a few pages and it would be good for learning. 我想从头开始将我的ASP.NET网页项目“升级”到ASP.NET MVC项目,它只有几页,对学习很有帮助。

The Issue 问题
The current project holds a few users for authentication ASP.NET made a few tables in my database: 当前项目拥有一些用于身份验证的用户ASP.NET在我的数据库中建立了一些表:

  • Roles 的角色
  • Profiles 个人资料
  • Users 用户数
  • ... ...

But when I use the MVC Project I see it makes different tables for authentication: 但是当我使用MVC项目时,我看到它为身份验证创建了不同的表:

  • AspNetUserLogins AspNetUserLogins
  • AspNetUserRoles AspNetUserRoles
  • AspNetUser AspNetUser
  • ... ...

The Question 问题
How do I migrate my users or how do I tell ASP.net to use the old tables? 如何迁移用户或如何告诉ASP.net使用旧表?

Greetings you may want to start with Database first Approach in MVC-Core but its little bit different from what you have seen so far, let me explain the steps for adding your database: 您可能想从MVC-Core数据库优先方法开始,但与您到目前为止所看到的略有不同,请允许我解释一下添加数据库的步骤:

  • Install Entity Framework: 安装实体框架:

Run Install-Package Microsoft.EntityFrameworkCore.SqlServer 运行Install-Package Microsoft.EntityFrameworkCore.SqlServer

To enable reverse engineering from an existing database we need to install a couple of other packages too. 为了从现有数据库中进行逆向工程,我们还需要安装其他两个软件包。

  1. Run Install-Package Microsoft.EntityFrameworkCore.Tools –Pre 运行Install-Package Microsoft.EntityFrameworkCore.Tools –Pre
  2. Run Install-Package Microsoft.EntityFrameworkCore.Design 运行安装包Microsoft.EntityFrameworkCore.Design
  3. Run Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design 运行安装包Microsoft.EntityFrameworkCore.SqlServer.Design

Now Reverse engineer your model: 现在对模型进行反向工程:

  • Run the following command to create a model from the existing database. 运行以下命令以从现有数据库创建模型。 If you receive an error stating the term 'Scaffold-DbContext' is not recognized as the name of a cmdlet, then close and reopen Visual Studio. 如果收到错误消息,指出术语'Scaffold-DbContext'未识别为cmdlet的名称,请关闭并重新打开Visual Studio。

Scaffold-DbContext "Server=(localdb)\\mssqllocaldb;Database=DataBaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

The reverse engineer process created entity classes and a derived context based on the schema of the existing database. 逆向工程过程基于现有数据库的架构创建实体类和派生上下文。 The entity classes are simple C# objects that represent the data you will be querying and saving. 实体类是简单的C#对象,它们代表您将要查询和保存的数据。 And now you can create a DbContext class and start using your database, so far you issue about database is over, and now i suggest you read about Identity In Asp.NET Core . 现在,您可以创建DbContext类并开始使用数据库,到目前为止,有关数据库的问题已经结束,现在,我建议您阅读有关Asp.NET Core中的Identity的信息

I came across this site thanks to this blog 多亏了这个博客,我才找到了这个网站

And edited to fit my needs: 并进行编辑以满足我的需求:

    /****** Object: Table [dbo].[AspNetRoles] Script Date: 11/14/2013 1:56:03 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('dbo.AspNetUserRoles', 'U') IS NOT NULL
    DROP TABLE [dbo].[AspNetUserRoles]
GO
IF OBJECT_ID('dbo.AspNetUserLogins', 'U') IS NOT NULL
    DROP TABLE [dbo].[AspNetUserLogins]
GO
IF OBJECT_ID('dbo.AspNetUserClaims', 'U') IS NOT NULL
    DROP TABLE [dbo].[AspNetUserClaims]
GO
IF OBJECT_ID('dbo.AspNetRoles', 'U') IS NOT NULL
    DROP TABLE [dbo].[AspNetRoles]
GO
IF OBJECT_ID('dbo.AspNetUsers', 'U') IS NOT NULL
    DROP TABLE [dbo].[AspNetUsers]
GO

CREATE TABLE [dbo].[AspNetUsers] (
    [AccessFailedCount]    INT            NOT NULL,
    [Email]                NVARCHAR (MAX) NULL,
    [EmailConfirmed]       BIT            DEFAULT ((0)) NULL,
    [Id]                   NVARCHAR (128) NOT NULL,
    [LockoutEnabled]       BIT            DEFAULT ((0)) NULL,
    [LockoutEndDateUtc]           DATETIME2 (7)  NULL,
    [PasswordHash]         NVARCHAR (MAX) NULL,
    [PhoneNumber]          NVARCHAR (MAX) NULL,
    [PhoneNumberConfirmed] BIT            DEFAULT ((0)) NULL,
    [SecurityStamp]        NVARCHAR (MAX) NULL,
    [TwoFactorEnabled]     BIT            DEFAULT ((0)) NULL,
    [UserName]             NVARCHAR (MAX) NULL,
    [CreateDate]                              DATETIME       NULL,
    [ConfirmationToken]                       NVARCHAR (128) NULL,
    [IsConfirmed]                             BIT            DEFAULT ((0)) NULL,
    [LastPasswordFailureDate]                 DATETIME       NULL,
    [PasswordFailuresSinceLastSuccess]        INT            DEFAULT ((0)) NULL,
    [PasswordChangedDate]                     DATETIME       NULL,
    [PasswordVerificationToken]               NVARCHAR (128) NULL,
    [PasswordVerificationTokenExpirationDate] DATETIME       NULL,
    CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE TABLE [dbo].[AspNetRoles] (
    [Id]   NVARCHAR (128) NOT NULL,
    [Name] NVARCHAR (MAX) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetRoles] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
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),
    CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId] FOREIGN KEY ([RoleId]) REFERENCES [dbo].[AspNetRoles] ([Id]) ON DELETE CASCADE,
    CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_RoleId]
    ON [dbo].[AspNetUserRoles]([RoleId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
    ON [dbo].[AspNetUserRoles]([UserId] ASC);
GO
CREATE TABLE [dbo].[AspNetUserLogins] (
    [UserId]        NVARCHAR (128) NOT NULL,
    [LoginProvider] NVARCHAR (128) NOT NULL,
    [ProviderKey]   NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUserLogins] PRIMARY KEY CLUSTERED ([UserId] ASC, [LoginProvider] ASC, [ProviderKey] ASC),
    CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
    ON [dbo].[AspNetUserLogins]([UserId] ASC);
GO
CREATE TABLE [dbo].[AspNetUserClaims] (
    [Id]         INT            IDENTITY (1, 1) NOT NULL,
    [ClaimType]  NVARCHAR (MAX) NULL,
    [ClaimValue] NVARCHAR (MAX) NULL,
    [UserId]    NVARCHAR (128) NOT NULL,
    CONSTRAINT [PK_dbo.AspNetUserClaims] PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_User_Id] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_User_Id]
    ON [dbo].[AspNetUserClaims]([UserId] ASC);
GO

INSERT INTO AspNetUsers(Id, UserName, PasswordHash, SecurityStamp,
CreateDate, ConfirmationToken, IsConfirmed, LastPasswordFailureDate, PasswordFailuresSinceLastSuccess,
PasswordChangedDate, PasswordVerificationToken, PasswordVerificationTokenExpirationDate,AccessFailedCount,LockoutEndDateUtc)
SELECT Users.UserId, Users.UserName, Memberships.Password, 
Memberships.PasswordSalt,  CreateDate, 
null, 1, '1/1/1977', 0,
'1/1/2017', null, '1/1/2100',0,'1/1/1977'
FROM Users
LEFT OUTER JOIN Memberships ON Users.UserId = Memberships.UserId
GO

INSERT INTO AspNetRoles(Id, Name)
SELECT RoleId, RoleName
FROM Roles
GO

INSERT INTO AspNetUserRoles(UserId, RoleId)
SELECT UserId, RoleId
FROM UsersInRoles
GO

--INSERT INTO AspNetUserLogins(UserId, LoginProvider, ProviderKey)
--SELECT UserId, Provider, ProviderUserId
--FROM Memberships
--GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将用户从自定义表迁移到ASP.NET成员资格表 - Migrate users from custom table to ASP.NET Membership tables 拒绝访问ASP.NET MVC Web API中的一组用户的特定页面 - Denying Access to specific pages for a set of users in ASP.NET MVC Web API 在ASP.NET MVC4网页上异步FTP上传 - Asynchronous FTP upload on ASP.NET MVC4 web pages 使用ASP.NET MVC 5动态创建新的网页/视图 - Create New Web Pages/Views Dynamically with ASP.NET MVC 5 Daterangepicker 在 Web 页面 ASP.NET MVC 中不起作用 - Daterangepicker is not working in Web Pages ASP.NET MVC 如何从ASP.NET Web窗体迁移到ASP.NET网页? 即/default.aspx到/ default - How to migrate from ASP.NET Web Forms to ASP.NET Web Pages? i.e /default.aspx to /default 在asp.net mvc中锁定用户之间的web对象 - Lock web object among users in asp.net mvc 无法迁移asp.net mvc 数据库 - Cannot migrate asp.net mvc database 如何将身份用户从 MVC5 应用程序迁移到 ASP.NET Core 2.2 应用程序 - How to migrate Identity users from a MVC5 app to a ASP.NET Core 2.2 app 如何在asp.net mvc中授权所有页面的用户并根据请求的类型(是否为ajax)进行响应? - how to authorize users in all pages and respond according to the type of request(ajax or not) in asp.net mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM