Asp.net mvc 自定义ASP.NET标识时出错-“;ApplicationUser不是当前上下文模型的一部分;

Asp.net mvc 自定义ASP.NET标识时出错-“;ApplicationUser不是当前上下文模型的一部分;,asp.net-mvc,ef-code-first,asp.net-identity,Asp.net Mvc,Ef Code First,Asp.net Identity,我已经阅读了很多关于这个错误的帖子/讨论,但没有找到一个明确的答案 我已经定制了ASP.NET标识来存储FirstName和Country以及用户名/密码/电子邮件地址,现在我在数据库中生成的表中存储这些信息时遇到了问题 我已经启用了迁移,添加了迁移,并更新了数据库,以反映我身份中的另外两个字段 在IdentityResult result=manager.Create(用户、型号、密码)处出错 web.config: <connectionStrings> <remo

我已经阅读了很多关于这个错误的帖子/讨论,但没有找到一个明确的答案

我已经定制了ASP.NET标识来存储
FirstName
Country
以及用户名/密码/电子邮件地址,现在我在数据库中生成的表中存储这些信息时遇到了问题

我已经启用了迁移,添加了迁移,并更新了数据库,以反映我身份中的另外两个字段

IdentityResult result=manager.Create(用户、型号、密码)处出错

web.config:

<connectionStrings>
    <remove name="DefaultConnection" />
    <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=WIN8-SUNEETAGU\SQLSERVER;Initial Catalog=UnderConstruction;Integrated Security=True" />
  </connectionStrings>

AuthenticationModels.cs

namespace UnderConstruction.Models
{
    public class AuthenticationModels
    {

        public class ApplicationUser : IdentityUser
        {
            public string FirstName { get; set; }

            public string Country { get; set; }
        }

        public class ApplicationDbContext : IdentityDbContext<UnderConstruction.Models.AuthenticationModels.ApplicationUser>
        {
            public ApplicationDbContext()

                : base("DefaultConnection")
            {}
        }

        public class RegisterModel
        {
            [Required]
            [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
            [Display(Name = "Username")]
            public string Username { get; set; }

            [Required]
            [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
            [DataType(DataType.Password)]
            [Display(Name = "Password")]
            public string Password { get; set; }

            [Required]
            [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
            [DataType(DataType.Password)]
            [Display(Name = "Confirm password")]
            public string ConfirmPassword { get; set; }

            [Required]
            [DataType(DataType.EmailAddress)]
            [EmailAddress(ErrorMessage="Please enter a valid email address.")]
            [Display(Name = "Email address")]
            public string Email { get; set; }

            [Required]
            [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
            [Display(Name = "First name")]
            public string FirstName { get; set; }

            [Required]
            [StringLength(50)]
            [Display(Name = "Country")]
            public string Country { get; set; }
        }

    }
}
命名空间欠构造。模型
{
公共类身份验证模型
{
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串国家{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{}
}
公共类注册器模型
{
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=5)]
[显示(Name=“Username”)]
公共字符串用户名{get;set;}
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=4)]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=4)]
[数据类型(数据类型.密码)]
[显示(Name=“确认密码”)]
公共字符串ConfirmPassword{get;set;}
[必需]
[数据类型(数据类型.电子邮件地址)]
[电子邮件地址(ErrorMessage=“请输入有效的电子邮件地址。”)]
[显示(Name=“电子邮件地址”)]
公共字符串电子邮件{get;set;}
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=5)]
[显示(Name=“First Name”)]
公共字符串名{get;set;}
[必需]
[长度(50)]
[显示(Name=“Country”)]
公共字符串国家{get;set;}
}
}
}
AuthenticationController.cs

public class AuthenticationController : Controller
    {
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(AuthenticationModels.RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {

                    var userStore = new UserStore<UnderConstruction.Models.AuthenticationModels.ApplicationUser>();
                    var manager = new UserManager<UnderConstruction.Models.AuthenticationModels.ApplicationUser>(userStore);

                    var user = new UnderConstruction.Models.AuthenticationModels.ApplicationUser() { UserName = model.Username, Email = model.Email, FirstName = model.FirstName, Country = model.Country };
                    IdentityResult result = manager.Create(user, model.Password);

                    return View("RegisterSuccessful");
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", e);
                }
            }

            return View("Register",model);
        }
    }
公共类身份验证控制器:控制器
{
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共操作结果寄存器(AuthenticationModels.RegisterModel模型)
{
if(ModelState.IsValid)
{
尝试
{
var userStore=new userStore();
var-manager=新的UserManager(userStore);
var user=new understruction.Models.AuthenticationModels.ApplicationUser(){UserName=model.UserName,Email=model.Email,FirstName=model.FirstName,Country=model.Country};
IdentityResult result=manager.Create(用户、型号、密码);
返回视图(“RegisterSuccessful”);
}
捕获(例外e)
{
AddModelError(“,e);
}
}
返回视图(“寄存器”,型号);
}
}

我认为这与AuthenticationModels.cs中的嵌套类有关。我会尝试将它们移到
AuthenticationModels
类之外。例如:

namespace UnderConstruction.Models.AuthenticationModels
{
    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }

        public string Country { get; set; }
    }

    public class ApplicationDbContext : IdentityDbContext<UnderConstruction.Models.AuthenticationModels.ApplicationUser>
    {
        public ApplicationDbContext()

            : base("DefaultConnection")
        {}
    }

    public class RegisterModel
    {
        [Required]
        [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
        [Display(Name = "Username")]
        public string Username { get; set; }

        [Required]
        [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [Required]
        [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 4)]
        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        public string ConfirmPassword { get; set; }

        [Required]
        [DataType(DataType.EmailAddress)]
        [EmailAddress(ErrorMessage="Please enter a valid email address.")]
        [Display(Name = "Email address")]
        public string Email { get; set; }

        [Required]
        [StringLength(10, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
        [Display(Name = "First name")]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50)]
        [Display(Name = "Country")]
        public string Country { get; set; }
    }   
}
命名空间Understruction.Models.AuthenticationModels
{
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串国家{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
公共应用程序上下文()
:base(“默认连接”)
{}
}
公共类注册器模型
{
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=5)]
[显示(Name=“Username”)]
公共字符串用户名{get;set;}
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=4)]
[数据类型(数据类型.密码)]
[显示(Name=“密码”)]
公共字符串密码{get;set;}
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=4)]
[数据类型(数据类型.密码)]
[显示(Name=“确认密码”)]
公共字符串ConfirmPassword{get;set;}
[必需]
[数据类型(数据类型.电子邮件地址)]
[电子邮件地址(ErrorMessage=“请输入有效的电子邮件地址。”)]
[显示(Name=“电子邮件地址”)]
公共字符串电子邮件{get;set;}
[必需]
[StringLength(10,ErrorMessage={0}必须至少有{2}个字符长。”,MinimumLength=5)]
[显示(Name=“First Name”)]
公共字符串名{get;set;}
[必需]
[长度(50)]
[显示(Name=“Country”)]
公共字符串国家{get;set;}
}   
}

我只是将所有内容移动到
Understruction.Models.AuthenticationModels
命名空间中,而不必创建
AuthenticationModels
类的实例。

也许我已经找到了解决方案。 检查Startup.Auth.cs中的UserManager初始化是否如下

UserManagerFactory = () => new UserManager<User>(new UserStore<User>(new MyContext()));
UserManagerFactory=()=>newusermanager(newuserstore(newmycontext());
您的sug
UserManagerFactory = () => new UserManager<User>(new UserStore<User>(new MyContext()));