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

前端 未结 1 1415
北荒
北荒 2020-12-20 22:05

I\'m trying (but failing) to implement the MVC UserManager to my website. I thought my case is fairly trivial. I have a User class (Entity Framewor

相关标签:
1条回答
  • 2020-12-20 22:44

    The problem is that you're trying to add a User you added to one DbContext to a ApplicationUser you add to a different DbContext (created by the UserManager).

    Instead, you should do this as a single operation. Just do this:

    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
    }
    
    0 讨论(0)
提交回复
热议问题