C# 未保存应用程序用户上的自定义字段

C# 未保存应用程序用户上的自定义字段,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,我在ApplicationUser类中添加了一些字段,现在当我试图保存用户时,它只保存电子邮件、密码和电话号码,因为这些是IdentityUser类中的一些原始字段 首先,也许这与这个问题有关。在我添加了字段之后,我向我的项目添加了一个迁移,但是这个迁移是空的,所以我手动键入代码来包含新字段,现在感觉EntityFramework不知怎么地无法识别类上的新字段,因此没有保存它 这是我为ApplicationUser类编写的代码 using System.Data.Entity; using Sy

我在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();
        }
    }
}

有什么想法吗?

如果有人有这个问题


我意识到ApplicationUser使用它自己的上下文,与我的其他模型类不同,因此我只是将迁移应用到错误的上下文,而不是ApplicationUser使用的上下文(如果有人遇到此问题)


我意识到ApplicationUser使用自己的上下文ApplicationDbContext,与我的其他模型类不同,因此我只是将迁移应用到错误的上下文,而不是ApplicationUser使用的上下文,你说你做了
添加迁移
。您是否也通过
更新数据库
<代码>添加迁移生成更改文件,
更新数据库
实际应用它。数据库中的模式是否已更新?是的,我也执行了更新数据库,但只有在迁移过程中手动添加模型更改后,我才能在AspNetUsers表中看到我的字段。感觉我的模型与表不“同步”,之所以添加这些列,是因为我在迁移时键入了它。当您执行
添加迁移时
,您是否确定您的目标是正确的程序集?还可以通过
Sql Profiler
或使用
Database.Log
DbContext
上设置日志记录。因此,您可以看到将什么SQL发送到db。是的,迁移的目标是正确的程序集。使用Sql分析器,我可以看到Insert命令和select查询没有我添加的字段,只有默认字段。另外,我在一个单独的项目中保存了我的IdentityModel,其中存储了我的所有模型和上下文,以防万一,这意味着你说你做了
添加迁移
。您是否也通过
更新数据库
<代码>添加迁移生成更改文件,
更新数据库
实际应用它。数据库中的模式是否已更新?是的,我也执行了更新数据库,但只有在迁移过程中手动添加模型更改后,我才能在AspNetUsers表中看到我的字段。感觉我的模型与表不“同步”,之所以添加这些列,是因为我在迁移时键入了它。当您执行
添加迁移时
,您是否确定您的目标是正确的程序集?还可以通过
Sql Profiler
或使用
Database.Log
DbContext
上设置日志记录。因此,您可以看到将什么SQL发送到db。是的,迁移的目标是正确的程序集。使用Sql分析器,我可以看到Insert命令和select查询没有我添加的字段,只有默认字段。另外,我在一个单独的项目上有我的IdentityModel,我在其中存储了我的所有模型和上下文,以防万一这意味着你不需要有两个单独的
DbContext
s。两者都应该使用同一个。@BrendanGreen,我发现这是我迁移的问题,但除此之外,我不知道如何解决这个问题。当ApplicationUser默认使用自己的ApplicationDbContext(IdentityDbContext)时,如何仅使用一个DbContext?对于我的其他模型,我有另一个DbContext,我正在与其他mvc 5项目共享。有什么想法吗?您不需要有两个独立的
DbContext
s。两者都应该使用同一个。@BrendanGreen,我发现这是我迁移的问题,但除此之外,我不知道如何解决这个问题。当ApplicationUser默认使用自己的ApplicationDbContext(IdentityDbContext)时,如何仅使用一个DbContext?对于我的其他模型,我有另一个DbContext,我正在与其他mvc 5项目共享。有什么想法吗?
        // 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);
        }
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