MVC - 附加ApplicationUser性能没有得到添加到AspNetUsers [英] MVC - Additional ApplicationUser properties not getting added to AspNetUsers

查看:693
本文介绍了MVC - 附加ApplicationUser性能没有得到添加到AspNetUsers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个使用MVC实体框架与单个用户帐户的新项目。我想名字和姓氏添加为用户的属性。 在这个博客量这些步骤:

I created a new project that is using MVC Entity Framework with Individual User Accounts. I want to add FirstName and LastName as properties of a user. The steps in this blog amount to these:

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表中的字段不存在,我不明白为什么

I did steps 1 - 4, but when I look at AspNetUsers table the fields are not there and I don't understand why.

这是我每步3所做的:

Public Class ApplicationUser
    Inherits IdentityUser

    Public FirstName As String
    Public LastName As String

但第4步后,这是我的表是什么样子:

But after step 4 this is what my table looks like:

在这里输入的形象描述

为什么不被添加到该预期表?

Why are FirstName and LastName not getting added to the table as expected?

这是什么在我的软件包管理器控制台:

This is what is in my Package Manager Console:

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表。

FirstName and LastName are not in "With" for the AspNetUsers table.

更新:

我从头重新开始,这一次我所做的就是创建迁移,那么手动添加名字和姓氏的AspNetUsers UP功能,然后跑更新数据库。这实际工作。

I restarted again from scratch, and this time what I did was create the Migration, then manually added FirstName and LastName in the AspNetUsers Up function, then ran update-database. That actually worked.

那么,为什么它没有自动这些字段添加到移民,我不知道。但是,如果我这样做手工,好像它工作的方式。

So why it didn't automatically add those fields to the migration, I don't know. But if I do that manually it seems like it works that way.

感谢您!

推荐答案

请参阅我张贴在回答<一个href=\"http://stackoverflow.com/questions/33223687/mvc-identity-wants-to-insert-null-into-table-when-value-is-not-null\">this问题我张贴随后这一个。

使用我能够以包括更多用户属性和它所有的作品!

With that I am able to include additional user properties and it all works!

无论这是否是最好的方式做到这一点可能是另外一个问题,但因为它本质上是一样的东西MVC最初提供我没有看到它的任何问题。

Whether or not that is the "best" way to do it may be another question, but since it's essentially the same as what MVC provides initially I don't see any issues with it.

这篇关于MVC - 附加ApplicationUser性能没有得到添加到AspNetUsers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆