更改身份表名称在Asp.Net MVC中不起作用

时间:2018-12-02 02:19:00

标签: c# asp.net model-view-controller asp.net-identity

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

我使用以下代码更改了身份表名称。(IdentityRoles转换为Roles)但是现在我不能使用IdentityRole table的新名称。当我使用新名称时,在该代码下会显示红线。

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

  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();
        }
    }

Error

0 个答案:

没有答案