首页 > 解决方案 > 更改身份表名称在 Asp.Net MVC 中不起作用

问题描述

命名空间“Microsoft.AspNet.Identity.EntityFramework”中不存在类型或命名空间名称“角色”

我使用下面的代码更改了 Identity 表名称。(IdentityRoles 到 Roles)但现在我不能使用 IdentityRole 表的新名称。当我使用新名称时,它会在该代码下显示红线。

// 我用于重命名身份表的代码

  public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    static string dbconn = (new ClinicDbContext()).Database.Connection.ConnectionString;
    public ApplicationDbContext()
        : base(dbconn, throwIfV1Schema: false)
    {
    }

    // Renaming Identity model tables name
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder); // This needs to go before the other rules!

        modelBuilder.Entity<ApplicationUser>().ToTable("Users");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserwithRole");


    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

// 下面这段代码有错误。我不能使用新名称Roles而不是IdentityRole

 [HttpPost]
    public ActionResult addrole(addrole Aroles)
    {
        try
        {
            var context = new Models.ApplicationDbContext();


            context.Roles.Add(new Microsoft.AspNet.Identity.EntityFramework.**Roles**()
            {
                Name = Aroles.name
            });
            context.SaveChanges();
            ViewBag.Message = "Role created successfully !";
            return RedirectToAction("Index", "Home");
        }
        catch
        {
            return View();
        }
    }

错误

标签: c#asp.netmodel-view-controllerasp.net-identity

解决方案


推荐阅读