名称空间“ 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();
}
}