MVC - 其他 ApplicationUser 属性未添加到 AspNetUsers

MVC - Additional ApplicationUser properties not getting added to AspNetUsers

我创建了一个使用 MVC Entity Framework 和个人用户帐户的新项目。我想添加 FirstName 和 LastName 作为用户的属性。 The steps in this blog 相当于这些:

1)  Create and run new Project
2)  Enable Migrations
3)  Add new Properties to ApplicationUser in IdentityModel
4)  Add Migration and Update Database
    - after this you can verify the new fields are in AspNetUsers table
5)  Update RegisterViewModel
6)  Update Register View
7)  Update Account Controller's Register Post

我执行了步骤 1 - 4,但是当我查看 AspNetUsers table 时,字段不存在,我不明白为什么。

这是我在第 3 步中所做的:

Public Class ApplicationUser
    Inherits IdentityUser

    Public FirstName As String
    Public LastName As String

但是在第 4 步之后,这就是我的 table 的样子:

为什么 FirstNameLastName 没有按预期添加到 table?

这是我的包管理器控制台中的内容:

PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project CustomUserProperties.
PM> add-migration "name"
Scaffolding migration 'name'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration name' again.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201510191912542_name].
Applying explicit migration: 201510191912542_name.
Running Seed method.
PM> 

这是迁移的内容:

Imports System
Imports System.Data.Entity.Migrations
Imports Microsoft.VisualBasic

Namespace Migrations
    Public Partial Class name
        Inherits DbMigration

        Public Overrides Sub Up()
            CreateTable(
                "dbo.AspNetRoles",
                Function(c) New With
                    {
                        .Id = c.String(nullable := False, maxLength := 128),
                        .Name = c.String(nullable := False, maxLength := 256)
                    }) _
                .PrimaryKey(Function(t) t.Id) _
                .Index(Function(t) t.Name, unique := True, name := "RoleNameIndex")

            CreateTable(
                "dbo.AspNetUserRoles",
                Function(c) New With
                    {
                        .UserId = c.String(nullable := False, maxLength := 128),
                        .RoleId = c.String(nullable := False, maxLength := 128)
                    }) _
                .PrimaryKey(Function(t) New With { t.UserId, t.RoleId }) _
                .ForeignKey("dbo.AspNetRoles", Function(t) t.RoleId, cascadeDelete := True) _
                .ForeignKey("dbo.AspNetUsers", Function(t) t.UserId, cascadeDelete := True) _
                .Index(Function(t) t.UserId) _
                .Index(Function(t) t.RoleId)

            CreateTable(
                "dbo.AspNetUsers",
                Function(c) New With
                    {
                        .Id = c.String(nullable := False, maxLength := 128),
                        .Email = c.String(maxLength := 256),
                        .EmailConfirmed = c.Boolean(nullable := False),
                        .PasswordHash = c.String(),
                        .SecurityStamp = c.String(),
                        .PhoneNumber = c.String(),
                        .PhoneNumberConfirmed = c.Boolean(nullable := False),
                        .TwoFactorEnabled = c.Boolean(nullable := False),
                        .LockoutEndDateUtc = c.DateTime(),
                        .LockoutEnabled = c.Boolean(nullable := False),
                        .AccessFailedCount = c.Int(nullable := False),
                        .UserName = c.String(nullable := False, maxLength := 256)
                    }) _
                .PrimaryKey(Function(t) t.Id) _
                .Index(Function(t) t.UserName, unique := True, name := "UserNameIndex")

            CreateTable(
                "dbo.AspNetUserClaims",
                Function(c) New With
                    {
                        .Id = c.Int(nullable := False, identity := True),
                        .UserId = c.String(nullable := False, maxLength := 128),
                        .ClaimType = c.String(),
                        .ClaimValue = c.String()
                    }) _
                .PrimaryKey(Function(t) t.Id) _
                .ForeignKey("dbo.AspNetUsers", Function(t) t.UserId, cascadeDelete := True) _
                .Index(Function(t) t.UserId)

            CreateTable(
                "dbo.AspNetUserLogins",
                Function(c) New With
                    {
                        .LoginProvider = c.String(nullable := False, maxLength := 128),
                        .ProviderKey = c.String(nullable := False, maxLength := 128),
                        .UserId = c.String(nullable := False, maxLength := 128)
                    }) _
                .PrimaryKey(Function(t) New With { t.LoginProvider, t.ProviderKey, t.UserId }) _
                .ForeignKey("dbo.AspNetUsers", Function(t) t.UserId, cascadeDelete := True) _
                .Index(Function(t) t.UserId)

        End Sub

        Public Overrides Sub Down()
            DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers")
            DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers")
            DropForeignKey("dbo.AspNetUserClaims", "UserId", "dbo.AspNetUsers")
            DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles")
            DropIndex("dbo.AspNetUserLogins", New String() { "UserId" })
            DropIndex("dbo.AspNetUserClaims", New String() { "UserId" })
            DropIndex("dbo.AspNetUsers", "UserNameIndex")
            DropIndex("dbo.AspNetUserRoles", New String() { "RoleId" })
            DropIndex("dbo.AspNetUserRoles", New String() { "UserId" })
            DropIndex("dbo.AspNetRoles", "RoleNameIndex")
            DropTable("dbo.AspNetUserLogins")
            DropTable("dbo.AspNetUserClaims")
            DropTable("dbo.AspNetUsers")
            DropTable("dbo.AspNetUserRoles")
            DropTable("dbo.AspNetRoles")
        End Sub
    End Class
End Namespace
对于 AspNetUsers table.

FirstNameLastName 不在 "With" 中

更新:

我再次从头开始,这次我做的是创建迁移,然后在 AspNetUsers Up 函数中手动添加 FirstName 和 LastName,然后 运行 update-database。这确实有效。

那么为什么它没有自动将这些字段添加到迁移中,我不知道。但如果我手动这样做,它似乎就是这样工作的。

谢谢!

查看我在 中发布的答案。

这样我就可以添加额外的用户属性,一切正常!

这是否是 "best" 的实现方式可能是另一个问题,但由于它与 MVC 最初提供的基本相同,所以我看不出有任何问题。