ApplicationUser - 我该如何使用它?我需要在我的使用条款中加入什么?

Neo*_*o42 6 asp.net-mvc entity-framework azure

我试图按照本教程,但我陷入了尝试添加基于ApplicationUser的代码的区域.角色部分.

dotnet将基于db的MVC站点部署到azure

我确实包括这些行:

    using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
Run Code Online (Sandbox Code Playgroud)

我的编译错误

Error 1 The type or namespace name 'ApplicationUser' could not be found (are you missing a using directive or an assembly reference?) c:\users\blah\documents\visual studio 2013\Projects\blah\blah\Migrations\Configuration.cs 96 38 blah

编辑:这是整个代码片段:

 bool AddUserAndRole(ContactManager.Models.ApplicationDbContext context)
 {
    IdentityResult ir;
    var rm = new RoleManager<IdentityRole>
        (new RoleStore<IdentityRole>(context));
    ir = rm.Create(new IdentityRole("canEdit"));
    var um = new UserManager<ApplicationUser>(
        new UserStore<ApplicationUser>(context));
    var user = new ApplicationUser()
    {
       UserName = "user1",
    };
    ir = um.Create(user, "Passw0rd1");
    if (ir.Succeeded == false)
       return ir.Succeeded;
    ir = um.AddToRole(user.Id, "canEdit");
    return ir.Succeeded;
 }
Run Code Online (Sandbox Code Playgroud)

Hor*_*Net 11

您是否创建了继承自IdentityUserApplicationUser类?如果是这样,您是否将ApplicationUser类所在的命名空间添加到Configuration类?

这是解决此错误最合理的事情.

  • `ApplicationUser`作为项目模板的一部分,它位于`Models/IdentityModels.cs`中.您需要添加一个`using`声明其在Configuration.cs命名空间(应该是`<项目> .Models`). (7认同)
  • 谢谢.为<project name> .models添加using语句就可以了.我倾向于将我的教程项目命名为我想要命名的内容(而不是盲目地遵循教程命名方案),在这种情况下它"让我陷入困境".但是我也学到了很多东西.所以我没有后悔. (3认同)