MVC 5 UserManager: The entity type ApplicationUser is not part of the model for the current context

asp.net-mvc c# entity-framework identity

Question

I'm attempting to implement the MVC, but I keep failing.UserManager to my web page. I believed my situation to be really simple. I possessUser class that holds all of the user's information (Entity Framework - Database first). Not all users of theUser possesses usage of the website. Consequently, there is a link to this class in my ApplicationUser. This is how it appears in code:

public partial class User
{
    public int Id { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
    public string Email { get; set; }
}

public class ApplicationUser : IdentityUser
{
    public virtual Entities.User UserInfo { get; set; }

    public ApplicationUser()
    {
        UserInfo = new Entities.User();
    }
}

Whenever using theCreateUserAsync It everything goes wrong when the controller's technique is used. Please find my controller code below.

public class AccountController : BaseController
{
    public UserManager<ApplicationUser> UserManager { get; private set; }

    public async Task<ActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var dbUser = new User 
            { 
                Firstname = model.FirstName, 
                Lastname = model.LastName, 
                Email = model.UserName 
            };
            db.Users.Add(dbUser);
            db.SaveChanges();

            var appUser = new ApplicationUser(model.UserName);
            appUser.UserInfo = dbUser;

            try
            {
                var result = await UserManager.CreateAsync(appUser, model.Password);
            }
            catch (Exception e)
            {
                db.Users.Remove(dbUser);
                db.SaveChanges();
            }
        }
    }
}

The CreateUserAsync -method produces the subsequent error:The entity type ApplicationUser is not part of the model for the current context.

Now for my inquiries:

  1. Is this how the "MVC" works? For something that is, in my judgment, inconsequential, I think this is a lot of labor.
  2. If not, is there a more effective method to accomplish this?
1
5
5/8/2015 1:51:03 PM

Accepted Answer

You are attempting to add a User that you added to one DbContext to an ApplicationUser that you added to a second DbContext, which is the issue (created by the UserManager).

Instead, you ought to carry out this as a single action. Put simply:

var dbUser = new User 
{ 
    Firstname = model.FirstName, 
    Lastname = model.LastName, 
    Email = model.UserName 
};
var appUser = new ApplicationUser(model.UserName);
appUser.UserInfo = dbUser;
try
{
   var result = await UserManager.CreateAsync(appUser, model.Password);
}
catch (Exception e)
{
   // show error or whatever
}
3
7/20/2014 8:35:06 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow