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

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

问题描述

我看到与这个的问题,但是在这里提出的方案似乎不适用,所以我认为我有一个不同的问题。事实上,我在SO上看到几个类似的问题,每个问题都有不同的原因和解决方案,所以我认为这个错误必须从高层次引起。这就是说...

I am seeing the same issue as this question, but the scenario presented there doesnt seem to apply so I think I have a different issue. In fact, I'm seeing several questions on SO that are similar, each with different causes and solutions, so I think this error must be caused from a high level. That said...

我有一个EF代码优先的数据库模型,我试图使用 IdentityUser 扩展我的MVC 5网站的标准注册。

I have an EF code-first database model and I'm trying to use IdentityUser to extend a standard registration for my MVC 5 site.

我有扩展的 UserModel

namespace MyMvcSite.Models {
    public class UserModel :IdentityUser {
        public string BillingId { get; set; }
        public virtual ICollection<DatabaseModel> Databases { get; set; }
}

我的上下文:

using MyMvcSite.Models;
namespace MyMvcSite.Web {
    public class AuthContext : IdentityDbContext<UserModel> {
        public AuthContext() : base("AuthContext") {

        }
    }
}

现在,当我执行注册用户的代码时:

Now, when I execute the code to register a user:

public async Task<IdentityResult> RegisterUser(UserModel user) {
    user.Email = user.UserName;
    var result = await _userManager.CreateAsync(user);

    return result;
}

我收到错误:实体类型IdentityUser是不是当前上下文的模型的一部分。我不知道这个错误是什么意思,因为它看起来像我一样 - 就像我有一切正确。有谁可以告诉可能出了什么问题?

I get the error: The entity type IdentityUser is not part of the model for the current context. I can't figure out what this error means, because it looks - to me - like I have everything correct. Can anyone tell what might be going wrong???

我知道我的connectionString AuthContext 是正确的,因为我已经使用

I know my connectionString AuthContext is correct because I have used it previously.

推荐答案

当您使用具有ASP.NET身份的自定义用户类时,必须确保您明确在 UserManager UserStore c中指定自定义用户类类型< T> / code>实例化。

When you are using a custom user class with ASP.NET Identity, you have to make sure that you explicitly specify the custom user class type <T> to both the UserManager and the UserStore on instantiation.

private UserManager<UserModel> _userManager;

public AccountController()
{
    AuthContext _ctx = new AuthContext();

    UserStore<UserModel> userStore = new UserStore<UserModel>(_ctx);
    _userManager = new UserManager<UserModel>(userStore);     
}

或更短的形式(如您的回复评论):

or in shorter form (like your reply comment):

private UserManager<UserModel> _userManager;

public AccountController()
{
    AuthContext _ctx = new AuthContext();    
    _userManager = new UserManager<UserModel>(new UserStore<UserModel>(_ctx));     
}

如果允许该类型默认为 IdentityUser 当您想使用自定义类时,您将遇到您报告的错误。

If the type is allowed to defaulted to IdentityUser when you want to use a custom class you will experience the error you reported.

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

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