MVC 5 UserManager:实体类型ApplicationUser不是当前上下文模型的一部分 [英] MVC 5 UserManager: The entity type ApplicationUser is not part of the model for the current context

查看:156
本文介绍了MVC 5 UserManager:实体类型ApplicationUser不是当前上下文模型的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试(但未能)将MVC UserManager实施到我的网站.我以为我的案子微不足道.我有一个User类(实体框架-数据库优先),其中包含所有用户的信息.并非User中的每个用户都可以访问该网站.因此,在我的ApplicationUser中,有一个指向此类的链接.在代码中,它看起来像这样:

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 Framework - Database first) which contains all the user's info. Not every user from the User has access to the website. So in my ApplicationUser there is a link to this class. In code it looks like this:

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

在控制器中调用CreateUserAsync方法时,一切都会出错.您可以在下面找到我的控制器代码.

When invoking the CreateUserAsync-method in the controller, it all goes wrong. Below you can find my controller code.

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

CreateUserAsync方法给出以下错误:The entity type ApplicationUser is not part of the model for the current context.

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

现在我的问题:

  1. 我是以"mvc"方式进行此操作吗?我觉得这对于我认为微不足道的事情来说是很多工作.
  2. 如果没有,是否有更好的方法来实现这一目标?

推荐答案

问题是您试图将添加到一个DbContext的用户添加到添加到不同DbContext(由UserManager创建)的ApplicationUser中.

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
}

这篇关于MVC 5 UserManager:实体类型ApplicationUser不是当前上下文模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆