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

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

问题描述

我正在从 Identity 1.0.0 迁移到 Identity 2.0.1 之后 文章

I'm migrating from Identity 1.0.0 to Identity 2.0.1 following this article

生成的迁移代码与新的 IdentityUser 无关.它不会添加新列.

and the migrations code generated is nothing about the new IdentityUser. It doesn't add the new columns.

所以我创建了一个新项目并再次尝试,但迁移代码为空.

So I made a new project and tried again but the migrations codes is empty.

为了解决这个问题,我直接在 SQL Server 中进行了编辑,然后在我的解决方案中再次导入了我的数据库.

To fix that problem, I did the edits directly in SQL Server and imported my database again in my solution.

现在我的AspNetUser和我的IdentityUser完全一样

Now my AspNetUser is exactly the same as my IdentityUser as you can see

身份用户

public virtual int AccessFailedCount { get; set; }

public virtual ICollection<TClaim> Claims { get; }

public virtual string Email { get; set; }

public virtual bool EmailConfirmed { get; set; }

public virtual TKey Id { get; set; }

public virtual bool LockoutEnabled { get; set; }

public virtual DateTime? LockoutEndDateUtc { get; set; }

public virtual ICollection<TLogin> Logins { get; }

public virtual string PasswordHash { get; set; }

public virtual string PhoneNumber { get; set; }

public virtual bool PhoneNumberConfirmed { get; set; }

public virtual ICollection<TRole> Roles { get; }

public virtual string SecurityStamp { get; set; }

public virtual bool TwoFactorEnabled { get; set; }

public virtual string UserName { get; set; }

IdentityUser.cs

public class ApplicationUser : IdentityUser
{
    public bool Has_accepted_policy { get; set; }
    public int user_type_id { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {

    }
}

AspNetUser

public string Id { get; set; }

[Required]
[StringLength(256)]
public string UserName { get; set; }

public string PasswordHash { get; set; }

public string SecurityStamp { get; set; }

[StringLength(256)]
public string Email { get; set; }

public bool EmailConfirmed { get; set; }

public bool Is_Active { get; set; }

[Required]
[StringLength(128)]
public string Discriminator { get; set; }

public int? user_type_id { get; set; }

public bool Has_accepted_policy { get; set; }

public string PhoneNumber { get; set; }

public bool PhoneNumberConfirmed { get; set; }

public bool TwoFactorEnabled { get; set; }

public DateTime? LockoutEndDateUtc { get; set; }

public bool LockoutEnabled { get; set; }

public int AccessFailedCount { get; set; }

... other virtual properties 

当我尝试注册用户时,出现以下异常

and when I try to register a user I have the following exception

实体类型 ApplicationUser 不是当前上下文模型的一部分

The entity type ApplicationUser is not part of the model for the current context

在这一行

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

我的startup.Auth.cs

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

在我的 AccountController 中,我像这样声明我的 UserManager

And in my AccountController I declare my UserManager like this

public AccountController()
    : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
{
}

public AccountController(UserManager<ApplicationUser> userManager,
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
    UserManager = userManager;
    AccessTokenFormat = accessTokenFormat;
}

public UserManager<ApplicationUser> UserManager { get; private set; }

除了 AspNetUser 类中的新属性外,我没有更改任何内容,并且它在迁移之前运行良好.

I haven't changed anything except the new properties in the AspNetUser class and it used to work well before the migration.

CodePlex 上有一个类似的问题,但他们没有给出解决方案

There's a similar issue on CodePlex marked as fixed but they don't give the solution

有人知道如何解决这个问题吗?

Does anyone know how to fix this?

编辑

为了确保我在编辑 SQL 数据库时没有犯任何错误.我创建了另一个项目并生成了一个身份数据库,我更改了该数据库的连接字符串,但仍然出现相同的错误.

To be sure I didn't do any mistakes when I edited my SQL database. I created another project and generated an Identity database and I changed the connection string for that database and I still have the same error.

解决方案

当我编辑我的数据库时,我没有注意到在 Identity 2.0.0 中他们更改了 AspUserClaims 表中 UserIdUser_Id.这样做之后,我遇到了同样的错误,但后来我按照 tschmit007 所说的将 ApplicationDbContext 添加到 UserStore 构造函数,现在它可以工作了.

When I have edited my database I haven't noticed that in Identity 2.0.0 they changed the User_Id for UserId in AspUserClaims table. After doing that I had the same error but then I did what tschmit007 said about adding the ApplicationDbContext to the UserStore constructor and now it works.

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

推荐答案

对我来说似乎缺少上下文实例化:

for me it seems to miss a context instanciation:

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

应该是

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

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

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