MVC 5在数据库中不自动创建AspNetUser表
本文关键字:创建 AspNetUser 数据库 MVC | 更新日期: 2023-09-27 18:07:10
我是MVC 5的新手,我正在尝试在创建的新数据库上下文中配置个人用户身份验证,但ApplicationDBContext没有在我的新数据库中创建AspNetUser表。有人能给我指出正确的方向吗?
错误
The entity type ApplicationUser is not part of the model for the current context.
IdentityModel.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
Web.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)'MSSQLLocalDB;AttachDbFilename=|DataDirectory|'aspnet-MVC5.WebSite-20150723075149.mdf;Initial Catalog=aspnet-MVC5.WebSite-20150723075149;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MyEntities" connectionString="metadata=res://*/Models.UserModel.csdl|res://*/Models.UserModel.ssdl|res://*/Models.UserModel.msl;provider=System.Data.SqlClient;provider connection string="data source=devserver;initial catalog=MyEntities;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
我知道MVC应该自动神奇地创建UserStore和相关联的表,但很明显,我在这里做了一些完全愚蠢的事情。非常感谢您的帮助。
您是否使用代码优先迁移?
通常,为了创建数据库,您必须启用迁移,创建新的迁移并应用它们。
进入软件包管理器控制台并执行以下命令
enable-migrations
add-migration initial
update-database
编辑
如果您不想使用迁移,也可以将其放在上下文的构造函数中:
Database.SetInitializer<ApplicationDbContext>(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
但每次重新创建数据库时,您都会丢失数据。
好吧,我在做一些愚蠢的事情。基本上是试图让ApplicationDbContext使用EF创建的连接。不是正确的方式。让表"自动神奇地"创建。我需要在Web.config中添加一个正常的连接字符串,连接到所需的DB,然后在IdentityModel.cs 中设置它
Web.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)'MSSQLLocalDB;AttachDbFilename=|DataDirectory|'aspnet-MVC.WebSite-20150723075149.mdf;Initial Catalog=aspnet-MVC.WebSite-20150723075149;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MyConnection" connectionString="Data Source=devserver;initial catalog=MyCatalog;user id=user;password=pass;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
IdentityModel.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
希望这能帮助到别人。感谢所有提供帮助的人。
如果需要重新创建表,下面是ASP.net EF 2.2 的生成脚本
USE [YourDatabaseNameHere]
GO
/****** Object: Table [dbo].[AspNetRoles] Script Date: 3/27/2019 3:42:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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]
GO
/****** Object: Table [dbo].[AspNetUserClaims] Script Date: 3/27/2019 3:42:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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]
GO
/****** Object: Table [dbo].[AspNetUserLogins] Script Date: 3/27/2019 3:42:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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]
GO
/****** Object: Table [dbo].[AspNetUserRoles] Script Date: 3/27/2019 3:42:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
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
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[AspNetUsers] Script Date: 3/27/2019 3:42:37 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
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]
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].[AspNetUserClaims] CHECK CONSTRAINT [FK_dbo.AspNetUserClaims_dbo.AspNetUsers_UserId]
GO
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].[AspNetUserLogins] CHECK CONSTRAINT [FK_dbo.AspNetUserLogins_dbo.AspNetUsers_UserId]
GO
ALTER TABLE [dbo].[AspNetUserRoles] WITH CHECK ADD CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[AspNetRoles] ([Id])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[AspNetUserRoles] CHECK CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetRoles_RoleId]
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
ALTER TABLE [dbo].[AspNetUserRoles] CHECK CONSTRAINT [FK_dbo.AspNetUserRoles_dbo.AspNetUsers_UserId]
GO
<add name="DefaultConnection" connectionString="Server=yourservername;Database=yourdbname;User Id=yourusername;Password=yourpassword;" providerName="System.Data.SqlClient" />
只需在web.config中创建此连接字符串,然后运行应用程序,而不是注册帐户。现在检查您的数据库。
我首先使用DB,修改了ExceptionLimeCat的答案,使其在我的系统上运行。如果答案未修改,它将不会注册,也不允许登录,也不会显示注册链接,也不会执行自动创建魔术,而是在强制执行时抛出异常。以下是对Web.config的修改,使其在中工作
<connectionStrings>
<add name="DefaultConnection" connectionString="data source=devserver;initial catalog=MyCatalog;user id=user;password=pass" providerName="System.Data.SqlClient" />
<add name="MyConnection" connectionString="metadata=res://*/Models.MyCatalog.csdl|res://*/Models.MyCatalog.ssdl|res://*/Models.MyCatalog.msl;provider=System.Data.SqlClient;provider connection string="data source=devserver;initial catalog=MyCatalog;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
这可能有助于那些发现上述答案复杂或有其他挑战的人。