我創建了一個新項目,該MVC Entity Framework與個人用戶帳戶一起使用。我想添加FirstName和LastName作爲用戶的屬性。 The steps in this blog量這些:MVC - 未添加到AspNetUsers的附加ApplicationUser屬性
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步後,這是我的表是什麼樣子:
爲什麼FirstName
和LastName
不會被添加到該該表如預期?
這是在我的包管理器控制檯:
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
FirstName
和LastName
不是「有了」爲AspNetUsers表。
UPDATE:
我從頭重新開始,這一次我所做的是創建遷移,然後手動添加名字和姓氏的AspNetUsers Up功能,然後跑更新數據庫。這實際上工作。
那麼,爲什麼它不會自動將這些字段添加到遷移中,我不知道。但是,如果我手動這樣做,它似乎就是這樣工作的。
謝謝!
聽起來像是沒有創建遷移或數據庫沒有更新。需要在Package Manager窗口中調用「add-migration」UserNames「'和'updata-database'。當你運行這些命令時,你有沒有得到任何警告或錯誤? – Sam
@Sam - 謝謝...我添加了PMC和Migration的詳細信息。對於該遷移中的AspNetUsers表,FirstName和LastName不在「With」中。不,我沒有得到任何錯誤,當我跑這些東西。任何想法爲什麼沒有這些額外的領域就可以創建遷移? – Andarta