如果您有现有数据库,并且要向其中包含Asp.NET Identity表,则可能会遇到此错误。同样在开始时,您可能不知道如何将 [AspNetRoles],[AspNetUserClaims],[AspNetUsers],[AspNetUserLogins] 表集成到现有数据库中。尽管有很多关于这个主题的资源,但这个答案是最短的答案之一。除了Asp.NET MVC的Asp.NET Identity功能之外,您可能希望在Entity Framework中使用Database-First方法。这是一个非常简短的假人教程。如果不好我很抱歉。
答案 0 :(得分:13)
以下是Asp.NET Identity表与现有数据库的最短集成。
1)在Visual Studio(2015或2013)中打开新项目或现有项目。打开服务器资源管理器并打开 DefaultConnection 。找到您的身份表。 (在WebConfig文件中,localDB connectionstring应该是活动的。并且您的其他现有数据库的连接字符串不应处于活动状态。)双击 [AspNetRoles],[AspNetUserClaims],[AspNetUsers],[AspNetUserLogins] < / strong>表格。并复制他们所有的SQL代码。
2)在SQL Server Management Studio中打开现有数据库,右键单击您的数据库,然后单击您在第1部分中复制的新查询。你会经历这样的事情:
CREATE TABLE [dbo].[AspNetRoles] (
[Id] NVARCHAR (128) NOT NULL,
[Name] NVARCHAR (256) NOT NULL,
CONSTRAINT [PK_dbo.AspNetRoles] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [RoleNameIndex]
ON [dbo].[AspNetRoles]([Name] ASC);
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)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
ON [dbo].[AspNetUsers]([UserName] ASC);
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_UserId]
ON [dbo].[AspNetUserRoles]([UserId] ASC);
GO
CREATE NONCLUSTERED INDEX [IX_RoleId]
ON [dbo].[AspNetUserRoles]([RoleId] ASC);
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),
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);
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),
CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId] FOREIGN KEY ([UserId]) REFERENCES [dbo].[AspNetUsers] ([Id]) ON DELETE CASCADE
);
GO
CREATE NONCLUSTERED INDEX [IX_UserId]
ON [dbo].[AspNetUserClaims]([UserId] ASC);
如果您忘记以GO开头的行,您将看到此问题标题中的错误。运行此查询并等待成功创建表。您现有的数据库现已准备好使用Asp.NET MVC 5的Identity功能。
3)在Visual Studio中打开WebConfig。我们将在这里更改连接字符串。写下这个:
<add name="DefaultConnection" connectionString="Data Source=YOUR_SERVER_NAME;Initial Catalog=YOUR_DATABASE_NAME;Persist Security Info=True;User ID=YOUR_USER_ID;Password=YOUR_PASSWORD.;MultipleActiveResultSets=True;Application Name=EntityFramework" providerName="System.Data.SqlClient"/>
而不是localDB连接字符串。这是:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-....mdf;Initial Catalog=aspnet-...;Integrated Security=True" providerName="System.Data.SqlClient" />
这就是你需要做的一切。运行您的项目并注册。您可以在现有数据库的AspNetUsers表中查看新用户数据。
答案 1 :(得分:4)
在Visual Studio中,转到&#34;工具 - &gt; NuGet包管理器 - &gt;包管理器控制台&#34;并在控制台窗口(在Visual Studio内)执行&#34;更新 - 数据库&#34;命令
答案 2 :(得分:3)
我经历过这个
有些时候用户为会员创建自定义类,因此asp.net不会创建AspNetUsers表
例如:
public class Member : IdentityUser
{
Public int MemberID { get; set; }
public string Name { get; set; }
public string Family { get; set; }
public string Password { get; set; }
}
在这种情况下,ASP.NET创建成员表,当用户想要进行身份验证时,ASP.NET搜索AspNetUsers并没有找到此表。 在自定义类
的开头添加TableAttribute解决了这个问题[Table("AspNetUsers")]
public class Member : IdentityUser
{
Public int MemberID { get; set; }
public string Name { get; set; }
public string Family { get; set; }
public string Password { get; set; }
}
答案 3 :(得分:1)
类似于上述内容,您可以在ConfigureServices中完成此操作
services.Configure<ApplicationDbContext>(o =>
{
// Make sure the identity database is created
.o.Database.Migrate();
});
// Applies any pending migrations for the context to the database. Will create the
// database if it does not already exist.
// Note that this API is mutually exclusive with DbContext.Database.EnsureCreated().
// EnsureCreated does not use migrations to create the database and therefore the
// database that is created cannot be later updated using migrations.
答案 4 :(得分:0)
我遇到了同样的问题,在这里我忘了打电话给 EnsureCreated()。 调用此方法后,它将创建Identity所需的所有表。
Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider serviceProvider)
{
...
// Make sure we have the database
serviceProvider.GetService<ApplicationDbContext>().Database.EnsureCreated();
}
答案 5 :(得分:0)
删除文件夹/.vs/[solution_name]/DesignTimeBuild。 命令行中的下一个:dotnet ef数据库更新--context [your_db_context_name] 它对我有用。
答案 6 :(得分:-2)
最快的方法是重新创建一个新项目,然后选择个人身份验证。然后使用旧文件代替新文件。