实体类型ApplicationUser不是当前上下文的模型的一部分 [英] The entity type ApplicationUser is not part of the model for the current context
问题描述
我正在从Identity 1.0.0迁移到Identity 2.0.1之后,这个文章
,并且生成的迁移代码与新的IdentityUser无关。它不添加新列。
所以我做了一个新项目,再次尝试,但迁移代码是空的。
要解决此问题,我直接在SQL Server中进行了编辑,并在我的解决方案中再次导入了我的数据库。
现在我的 AspNetUser
与我的 IdentityUser
您可以看到
IdentityUser
public virtual int AccessFailedCount {get;组; }
public virtual ICollection< TClaim>索赔{get; }
public virtual string Email {get;组; }
public virtual bool EmailConfirmed {get;组; }
public virtual TKey Id {get;组; }
public virtual bool LockoutEnabled {get;组; }
public virtual DateTime? LockoutEndDateUtc {get;组; }
public virtual ICollection< TLogin>登录{get; }
public virtual string PasswordHash {get;组; }
public virtual string PhoneNumber {get;组; }
public virtual bool PhoneNumberConfirmed {get;组; }
public virtual ICollection< TRole>角色{get; }
public virtual string SecurityStamp {get;组; }
public virtual bool TwoFactorEnabled {get;组; }
public virtual string UserName {get;组;
IdentityUser.cs
public class ApplicationUser:IdentityUser
{
public bool Has_accepted_policy {get;组; }
public int user_type_id {get;组; }
}
public class ApplicationDbContext:IdentityDbContext< ApplicationUser>
{
public ApplicationDbContext()
:base(DefaultConnection)
{
}
}
AspNetUser
public string Id {get;组;
[必需]
[StringLength(256)]
public string UserName {get;组; }
public string PasswordHash {get;组; }
public string SecurityStamp {get;组;
[StringLength(256)]
public string Email {get;组; }
public bool EmailConfirmed {get;组; }
public bool Is_Active {get;组;
[必需]
[StringLength(128)]
public string Discriminator {get;组; }
public int? user_type_id {get;组; }
public bool Has_accepted_policy {get;组; }
public string PhoneNumber {get;组; }
public bool PhoneNumberConfirmed {get;组; }
public bool TwoFactorEnabled {get;组; }
public DateTime? LockoutEndDateUtc {get;组; }
public bool LockoutEnabled {get;组; }
public int AccessFailedCount {get;组; }
...其他虚拟属性
当我尝试注册一个用户我有以下异常
实体类型ApplicationUser不是当前上下文模型的一部分
在这一行
IdentityResult result = await UserManager.CreateAsync (user,model.Password);
我的 startup.Auth.cs
UserManagerFactory =()=> new UserManager< ApplicationUser>(new UserStore< ApplicationUser>());
在我的 AccountController 中,我声明了我的 UserManager
这样
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;私人集合$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
/ code> class,它在迁移之前用起来很好。
CodePlex还有一个类似的问题标记为固定的,但他们没有给出解决方案
有谁知道如何解决这个问题?
编辑
确保在编辑SQL数据库时没有发生任何错误。我创建了另一个项目并生成了一个Identity数据库,我更改了该数据库的连接字符串,我仍然有相同的错误。
解决方案 / p>
当我编辑我的数据库时,我没有注意到,在Identity 2.0.0中,他们更改了
User_Id
code> UserIdAspUserClaims
表中。做完之后,我有同样的错误,但是我做了tschmit007所说的关于将ApplicationDbContext
添加到UserStore
构造函数中,现在它的作品UserManagerFactory =()=> new UserManager< ApplicationUser>(new UserStore< ApplicationUser>(new ApplicationDbContext()));
解决方案对我来说似乎错过了一个上下文instanciation: / p>
UserManagerFactory =()=> new UserManager< ApplicationUser>(new UserStore< ApplicationUser>());
应该是
code> UserManagerFactory =()=> new UserManager< ApplicationUser>(new UserStore< ApplicationUser>(new ApplicationDbContext()));
I'm migrating from Identity 1.0.0 to Identity 2.0.1 following this article
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.
To fix that problem, I did the edits directly in SQL Server and imported my database again in my solution.
Now my
AspNetUser
is exactly the same as myIdentityUser
as you can seeIdentityUser
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
The entity type ApplicationUser is not part of the model for the current context
at this line
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
My startup.Auth.cs
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
And in my AccountController I declare my
UserManager
like thispublic 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; }
I haven't changed anything except the new properties in the
AspNetUser
class and it used to work well before the migration.There's a similar issue on CodePlex marked as fixed but they don't give the solution
Does anyone know how to fix this?
EDIT
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.
SOLUTION
When I have edited my database I haven't noticed that in Identity 2.0.0 they changed the
User_Id
forUserId
inAspUserClaims
table. After doing that I had the same error but then I did what tschmit007 said about adding theApplicationDbContext
to theUserStore
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>());
should be
UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
这篇关于实体类型ApplicationUser不是当前上下文的模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!