【问题标题】:Asp.net Identity 2 The entity type User is not part of the model for the current contextAsp.net Identity 2 实体类型用户不是当前上下文模型的一部分
【发布时间】:2014-07-25 16:47:50
【问题描述】:

我的解决方案中有 3 个项目,域、API 和 Web。最近我已将 Asp.net Identity 表单 1.0 更新为 2.0。在我的 Web 项目中一切正常,但是当我尝试在 Web API 项目中获取 Token 时出现此错误(在将 Identity 从 1.0 升级到 2.0 之前一切正常):

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

我的用户类如下所示:

public class User : IdentityUser
{
   //more code here
}

这是我的数据库上下文类:

    public class DatabaseContext : IdentityDbContext<User>
        {
            public DatabaseContext()
                : base("DefaultConnection", throwIfV1Schema: false)
            {
                Configuration.LazyLoadingEnabled = true;
            }

          //more code here

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>()
                .ToTable("AspNetUsers");
            modelBuilder.Entity<User>()
                .ToTable("AspNetUsers");
        }

    }

在我的 Web API 项目中,我已将 IdentityUser 中的所有引用替换为 User,例如获取 Token 的方法如下:

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        using (UserManager<User> userManager = _userManagerFactory())
        {
            var user =   userManager.Find(context.UserName, context.Password);

            if (user == null)
            {
                context.SetError("invalid_grant", "The user name or password is incorrect.");
                return;
            }

            ClaimsIdentity oAuthIdentity = await userManager.CreateIdentityAsync(user,
                context.Options.AuthenticationType);
            ClaimsIdentity cookiesIdentity = await userManager.CreateIdentityAsync(user,
                CookieAuthenticationDefaults.AuthenticationType);
            AuthenticationProperties properties = CreateProperties(user.UserName);
            AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
        }
    }

我该如何解决这个问题?

【问题讨论】:

  • 我认为 IdentityDbContext 为给定的用户类型创建了一个 DbSet,所以我不确定这是否正确 - 但尝试使用 modelBuilder.Entity&lt;User&gt;()... 而不是 modelBuilder.Entity&lt;IdentityUser&gt;()...
  • 我都有,但我删除了modelBuilder.Entity(),只留下了modelBuilder.Entity(),但没有帮助

标签: c# asp.net asp.net-web-api asp.net-identity asp.net-identity-2


【解决方案1】:

我已经解决了,

所以问题在于将 Asp.net Identity 从 1.0 更新到 2.0,因为 ASP.net Web API 对 2.0 的处理方式有所不同,所以我所做的是:

  1. 已更新 VS 2013(更新 2)
  2. 在另一个解决方案中创建了新的 Web API 2 项目,并将其与我的 Web API 项目进行了比较
    并为其添加了缺失的类(只需复制/粘贴)
  3. 在我的 DatabaseContext 类中,我添加了方法:

    公共静态数据库上下文创建() { 返回新的数据库上下文(); }

  4. 对于我的用户类:

    公共异步任务 GenerateUserIdentityAsync(UserManager manager, string authenticationType) { // 注意 authenticationType 必须与 CookieAuthenticationOptions.AuthenticationType 中定义的一致 var userIdentity = await manager.CreateIdentityAsync(this, authenticationType); // 在此处添加自定义用户声明 返回用户身份; }

  5. 我已经用我的用户参考更改了 Web API 项目中的所有 ApplicationUser 参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2016-08-31
    • 2017-06-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多