我在ApplicationUser類中添加了一些字段,現在當我試圖保存用戶時,它只保存電子郵件,密碼和電話號碼,因爲這些字段是一些原始字段IdentityUser類。ApplicationUser上的自定義字段沒有被保存
首先,也許這是與問題有關。添加字段後,我添加了一個遷移到我的項目,但這個遷移是空的,所以我手動鍵入代碼以包含新字段,現在感覺像某種方式EntityFramework不能識別我的類上的新字段,因此不保存它。
這是我的ApplicationUser類
using System.Data.Entity;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
namespace Domain.Models
{
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public string FirstName;
public string LastName;
public string Photo;
public string Street;
public string City;
public string State;
public string Country;
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("CubaViajes", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
}
代碼,這是我創建用戶並保存方法
// POST: /Account/Register
[HttpPost]
[ValidateAntiForgeryToken]
[AllowAnonymous]
public async Task<ActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser {
UserName = model.Email,
Email = model.Email,
FirstName=model.FirstName,
LastName=model.LastName,
PhoneNumber=model.Phone,
Street=model.Street,
City=model.City,
State=model.State,
Country=model.Country
};
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
//Assign Role to user Here
await this.UserManager.AddToRoleAsync(user.Id, model.Rol);
//Ends Here
await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
Session["userAdded"] = true;
return RedirectToAction("Index", "Account");
}
AddErrors(result);
}
ViewBag.Roles = new SelectList(context.Roles.ToList(), "Name", "Name");
// If we got this far, something failed, redisplay form
return View(model);
}
用戶被成功保存,但只保存電子郵件,密碼和電話號碼,並沒有堅持我擁有的其他領域。
編輯1:
我檢查SQL事件探查器和INSERT命令的新領域未列出,供選擇查詢一樣,只檢索/保存默認設置一個
下面是選擇查詢不包括我添加的字段
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Email] AS [Email],
[Extent1].[EmailConfirmed] AS [EmailConfirmed],
[Extent1].[PasswordHash] AS [PasswordHash],
[Extent1].[SecurityStamp] AS [SecurityStamp],
[Extent1].[PhoneNumber] AS [PhoneNumber],
[Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed],
[Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled],
[Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc],
[Extent1].[LockoutEnabled] AS [LockoutEnabled],
[Extent1].[AccessFailedCount] AS [AccessFailedCount],
[Extent1].[UserName] AS [UserName]
FROM [dbo].[AspNetUsers] AS [Extent1]
go
有沒有想法?
謝謝
用一個你說你做一個'添加遷移'。你是否也用「更新數據庫」跟進了這個問題? 'Add-Migration'生成更改文件,'Update-Database'實際上應用它。數據庫中的模式是否已更新? – rism
是的,我也執行了更新數據庫,但只有在我必須在遷移中手動添加模型更改後,我才能在AspNetUsers表上看到我的字段存在 –
感覺好像我的模型不與「同步」表中,只添加了列,因爲我在遷移時輸入了它 –