0

I have read lots of other posts/discussions regarding this error and have not found a clear answer.

I have customised ASP.NET Identity to store FirstName and Country as well as username/password/email address, and I'm now having issues actually storing this within the generated tables within my database.

I have already enabled migrations, added a migration and updated the database to reflect the 2 additional fields within my Identity.

Error at IdentityResult result = manager.Create(user, model.Password);

$exception  {"The entity type ApplicationUser is not part of the model for the current context."}   System.Exception {System.InvalidOperationException}

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; }
        }

    }
}

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);
        }
    }
litterbugkid
  • 3,534
  • 7
  • 36
  • 54
  • How did you create the base project? Did you use the MVC 5 template in VS 2013 and select "Individual User Accounts" for "Authentication"? Or some other method? – Kevin Junghans Apr 01 '14 at 14:42
  • your userstore constructor should be using your ApplicationDbContext class, var userstore = new UserStore(new ApplicationDbContext()); – Suhas Joshi Apr 01 '14 at 17:27
  • @KevinJunghans I didn't use the MVC5 template. I installed MVC5 but for some reason no other templates appeared. Could you show me a valid MVC5 download link? – litterbugkid Apr 02 '14 at 08:15
  • @SuhasJoshi Thank you! That worked! If you put your reply in a proper answer I'll accept it and give you the credit. – litterbugkid Apr 02 '14 at 08:21
  • @Neeta I am assuming from your response that you are using VS 2012 to create an MVC 5 project. Take a look at this QA and see if it helps.http://stackoverflow.com/questions/17968304/create-and-run-mvc-5-project-in-vs-2012 It looks like you have an answer to your issue. You should have a Startup.Auth.cs as Mino mentioned though. – Kevin Junghans Apr 02 '14 at 12:36

2 Answers2

0

I'm thinking it has to do with the nested classes within AuthenticationModels.cs. I'd try moving them outside of the AuthenticationModels class. Example:

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; }
    }   
}

I just moved everything into the UnderConstruction.Models.AuthenticationModels namespace, instead of having to create an instance of the AuthenticationModels class.

Steven V
  • 16,357
  • 3
  • 63
  • 76
0

Maybe I've found a solution. Check the UserManager initialization in Startup.Auth.cs to be like this

UserManagerFactory = () => new UserManager<User>(new UserStore<User>(new MyContext()));
Mino
  • 635
  • 10
  • 28