[英]ASP.Net Identity - Use custom Schema
我首先将 MVC5 + Ef6 代码与 ASP.Net Identity 1.0 一起使用,并希望在自定义架构中创建表。 即不是 dbo 模式的模式。
我使用 Ef 电源工具对我的数据库进行了逆向工程,并将映射类中所有其他表的模式名称设置为以下内容
this.ToTable("tableName", "schemaName");
我尝试为 ASP.Net 表执行此操作,但它不断给我带来很多错误,最终我放弃了。 如果我从我的项目中排除(逆向工程)ASP.Net Identity 表,它们将被创建,但总是在 dbo 模式中
有人知道怎么做吗?
public class MyDbContext : EntityDbContext<ApplicationUser>
{
public DbSet<ApplicationUser> Users { get; set; }
public MyDbContext() : base()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// You can globally assign schema here
modelBuilder.HasDefaultSchema("schemaName");
}
}
这是一个解释我做了什么的迟到条目。 不确定是否有更好的方法,但这是唯一对我有用的方法。
公平地说,在我的上下文中,我有不止一个模型。 这就是为什么这对我更好。
add-migration
并让它创建一个迁移update-database
以更新这些更改add-migration
并让它创建一个新的迁移OnModelCreating
方法笔记:
你不想要这个。
// This globally assigned a new schema for me (for ALL models)
modelBuilder.HasDefaultSchema("security");
配置:OnModelCreating
这仅为提到的表分配了一个新模式
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers", "security");
modelBuilder.Entity<CustomRole>().ToTable("AspNetRoles", "security");
modelBuilder.Entity<CustomUserClaim>().ToTable("AspNetUserClaims", "security");
modelBuilder.Entity<CustomUserLogin>().ToTable("AspNetUserLogins", "security");
modelBuilder.Entity<CustomUserRole>().ToTable("AspNetUserRoles", "security");
}
初始迁移看起来像
public partial class Initial : DbMigration
{
public override void Up()
{
CreateTable(
"security.AspNetRoles",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex");
CreateTable(
"security.AspNetUserRoles",
c => new
{
UserId = c.String(nullable: false, maxLength: 128),
RoleId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.UserId, t.RoleId })
.ForeignKey("security.AspNetRoles", t => t.RoleId, cascadeDelete: true)
.ForeignKey("security.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId)
.Index(t => t.RoleId);
CreateTable(
"security.AspNetUsers",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
FirstName = c.String(nullable: false, maxLength: 250),
LastName = c.String(nullable: false, maxLength: 250),
Email = c.String(maxLength: 256),
EmailConfirmed = c.Boolean(nullable: false),
PasswordHash = c.String(),
SecurityStamp = c.String(),
PhoneNumber = c.String(),
PhoneNumberConfirmed = c.Boolean(nullable: false),
TwoFactorEnabled = c.Boolean(nullable: false),
LockoutEndDateUtc = c.DateTime(),
LockoutEnabled = c.Boolean(nullable: false),
AccessFailedCount = c.Int(nullable: false),
UserName = c.String(nullable: false, maxLength: 256),
})
.PrimaryKey(t => t.Id)
.Index(t => t.UserName, unique: true, name: "UserNameIndex");
CreateTable(
"security.AspNetUserClaims",
c => new
{
Id = c.Int(nullable: false, identity: true),
UserId = c.String(nullable: false, maxLength: 128),
ClaimType = c.String(),
ClaimValue = c.String(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("security.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
CreateTable(
"security.AspNetUserLogins",
c => new
{
LoginProvider = c.String(nullable: false, maxLength: 128),
ProviderKey = c.String(nullable: false, maxLength: 128),
UserId = c.String(nullable: false, maxLength: 128),
})
.PrimaryKey(t => new { t.LoginProvider, t.ProviderKey, t.UserId })
.ForeignKey("security.AspNetUsers", t => t.UserId, cascadeDelete: true)
.Index(t => t.UserId);
}
public override void Down()
{
DropForeignKey("security.AspNetUserRoles", "UserId", "security.AspNetUsers");
DropForeignKey("security.AspNetUserLogins", "UserId", "security.AspNetUsers");
DropForeignKey("security.AspNetUserClaims", "UserId", "security.AspNetUsers");
DropForeignKey("security.AspNetUserRoles", "RoleId", "security.AspNetRoles");
DropIndex("security.AspNetUserLogins", new[] { "UserId" });
DropIndex("security.AspNetUserClaims", new[] { "UserId" });
DropIndex("security.AspNetUsers", "UserNameIndex");
DropIndex("security.AspNetUserRoles", new[] { "RoleId" });
DropIndex("security.AspNetUserRoles", new[] { "UserId" });
DropIndex("security.AspNetRoles", "RoleNameIndex");
DropTable("security.AspNetUserLogins");
DropTable("security.AspNetUserClaims");
DropTable("security.AspNetUsers");
DropTable("security.AspNetUserRoles");
DropTable("security.AspNetRoles");
}
}
对不起,我的英语,我使用谷歌翻译。
零号囚徒指示的某些步骤不是必需的。 提供的指示基于具有个人用户帐户安全性的标准模板。
首先,我们必须验证我们的项目是否干净(在包管理控制台中插入命令):
Drop-Database
Remove-Migration
现在我们的项目是干净的,我们必须修改ApplicationDbContext
类。 我们必须覆盖方法OnModelCreating
以指示由 ASP.NET Identity 生成的每个表所属的方案。 以下链接显示了用于映射每个表的实体以及有关自定义构建器的信息和用于更改每个表的主键数据类型的选项: Identity Model Customization 。
public class ApplicationDbContext : IdentityDbContext {
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder builder) {
base.OnModelCreating(builder);
builder.Entity<IdentityUser>().ToTable("AspNetUsers", "myschema");
builder.Entity<IdentityRole>().ToTable("AspNetRoles", "myschema");
builder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims", "myschema");
builder.Entity<IdentityUserRole>().ToTable("AspNetUserRoles", "myschema");
builder.Entity<IdentityUserLogin>().ToTable("AspNetUserLogins", "myschema");
builder.Entity<IdentityRoleClaim>().ToTable("AspNetRoleClaims", "myschema");
builder.Entity<IdentityUserToken>().ToTable("AspNetUserTokens", "myschema");
}
}
现在我们只需要生成我们的迁移。 为此,在包管理控制台中输入以下命令(您可以选择使用-OutputDir
参数指示输出路由):
Add-Migration InitialSchemaIdentity -OutputDir Data\\Migrations
然后我们使用以下命令在我们的数据库中应用更改:
Update-Database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.