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

时间:2015-10-19 19:36:01

标签: asp.net-mvc vb.net entity-framework asp.net-identity

我创建了一个使用MVC实体框架和个人用户帐户的新项目。我想添加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表时,字段不存在,我不明白为什么。

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

Public Class ApplicationUser
    Inherits IdentityUser

    Public FirstName As String
    Public LastName As String

但是在第4步之后,这就是我的表格:

enter image description here

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

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

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

FirstNameLastName不在"用"对于AspNetUsers表。

更新

我从头再次重新启动,这次我做的是创建迁移,然后在AspNetUsers Up功能中手动添加FirstName和LastName,然后运行update-database。这实际上有效。

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

谢谢!

1 个答案:

答案 0 :(得分:0)

请参阅我在this questions I posted subsequent to this one中发布的答案。

有了这个,我可以包含其他用户属性,一切正常!

这是否是“最佳”方式可能是另一个问题,但由于它与MVC最初提供的内容基本相同,所以我认为没有任何问题。