小编典典

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

c#

我将根据本文从Identity
1.0.0迁移到Identity
2.0.1

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

因此,我做了一个新项目,然后再次尝试,但是迁移代码为空。

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

现在我和您看到的AspNetUser完全一样IdentityUser

身份用户

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

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

实体类型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; private set; }

除了AspNetUser该类中的新属性外,我没有进行任何其他更改,并且在迁移之前,它可以正常工作。

CodePlex上有一个类似的问题,标记为固定,但没有给出解决方案

有谁知道如何解决这一问题?

编辑

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

当我已经编辑我的数据库我还没有注意到,在身份2.0.0他们改变了User_IdUserIdAspUserClaims表中。完成此操作后,我遇到了相同的错误,但随后我做了tschmit007所说的关于将d添加ApplicationDbContextUserStore构造函数中的操作,现在它可以正常工作了。

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

阅读 845

收藏
2020-05-19

共1个答案

小编典典

对我来说,似乎错过了上下文说明:

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

应该

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
2020-05-19