c# - 更改身份表名称在 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();
}
}
解决方案
推荐阅读
- spring-boot - “状态”:403,“错误”:“禁止”,“消息”:“禁止”,“路径”:“/post/create”
- node.js - 从 mysql 安全/验证 Node js-Express Rest api 的方法
- python - 给定一个图(或元图)(但没有 TF 事件文件),如何可视化它
- php - RouteServiceProvider 中的 Laravel 显式路由模型绑定问题
- mysql - Mysql 不同的行 2 列
- c# - NSOutlineView 使用代码中的动画展开/折叠项目
- react-native - 使用 React Native 的 Geolocation watchPosition 和 redux 存储坐标,坐标获取不频繁不准确
- c# - 以 AJAX 和 JSON 显示数据
- c# - 我如何在不丢失结构的情况下更改表单 c# 的大小
- javascript - 从 javascript 访问 json 有效负载中的参数