ApplicationUser上的自定义字段没有保存

本文关键字:保存 字段 自定义 ApplicationUser | 更新日期: 2023-09-27 18:18:30

我添加了一些字段到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分析器,并在插入命令上未列出新字段,对于选择查询也是如此,仅检索/保存默认值

下面的

是不包含我添加的

字段的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

任何想法?

ApplicationUser上的自定义字段没有保存

如果有人有这个问题。

我意识到ApplicationUser使用自己的上下文ApplicationDbContext而不是与我的其他模型类相同,所以我只是将迁移应用到错误的上下文,而不是ApplicationUser

使用的上下文