我在ApplicationUser类中添加了一些字段,现在当我尝试保存用户时,它只保存了电子邮件,密码和电话号码,因为这些是IdentityUser类的一些原始字段。
首先,也许这与问题有关。在我添加了字段之后,我添加了一个迁移到我的项目,但是这个迁移是空的,所以我手动输入代码以包含新字段,现在感觉某种程度上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查询相同,只检索/保存默认值
下面是选择查询,它不包含我添加的字段
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
有什么想法吗?
感谢
答案 0 :(得分:0)
如果有人有此问题。
我意识到ApplicationUser使用自己的上下文 ApplicationDbContext 而不是我的其他模型类,所以我只是将迁移应用到错误的上下文而不是ApplicationUser使用的上下文