I am using MVC5 + Ef6 code first with ASP.Net Identity 1.0 and wish to have the tables created in a custom schema. i.e. a schema that is not the dbo schema.
我正在使用MVC5 + Ef6代码首先与ASP。Net Identity 1.0,并希望在自定义模式中创建表。即一个非dbo模式的模式。
I reversed engineered my databse using the Ef power tools and set the schema name for all other tables in the mapping class to the following
我使用Ef power工具反向工程了我的数据库,并将映射类中所有其他表的模式名设置为以下
this.ToTable("tableName", "schemaName");
I tried doing this for the ASP.Net tables but it kept giving me a lots of errors and eventually I gave up. If I exclude the (reverse engineered) ASP.Net Identity tables from my project they will be created but always in the dbo schema
我试着为ASP做这个。但它总是给我带来很多错误,最终我放弃了。如果我排除(反向工程)ASP。来自我的项目的Net标识表将被创建,但总是在dbo模式中
Anyone know how to do this?
有人知道怎么做吗?
2 个解决方案
#1
13
public class MyDbContext : EntityDbContext<ApplicationUser>
{
public DbSet<ApplicationUser> Users { get; set; }
public MyDbContext() : base()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// You can globally assign schema here
modelBuilder.HasDefaultSchema("schemaName");
}
}
#2
4
Here is a late entry explaining what I did. Not sure if there is a better way, but this is the ONLY thing that worked for me.
这里有一个很晚的条目解释我做了什么。不确定是否有更好的方法,但这是我唯一有效的方法。
To be fair, I have more than a single model in my context. Which is why this was better for me.
公平地说,在我的背景下,我有不止一个模型。这就是为什么这对我更好。
- Generate the tables in a database ahead of time (while tables are still in 'dbo')
- 提前在数据库中生成表(而表仍然在“dbo”中)
- Execute
add-migration
on your project and let it create a migration - 在项目上执行add-migration并让它创建一个迁移
- Change all the schemas within your migration code to the desired schema
- 将迁移代码中的所有模式更改为所需的模式
- Execute
update-database
to get those changes updated - 执行更新数据库以更新这些更改。
- Delete your original migration file (its' hash is useless to you)
- 删除原始迁移文件(它的散列对您没有用处)
- Execute
add-migration
again and let it create a new migration - 再次执行插件迁移,并让它创建一个新的迁移。
- Update the
OnModelCreating
method of your configuration with the code below - 使用下面的代码更新配置的onmodelcreation方法
- Run your application and start registering users
- 运行应用程序并开始注册用户
NOTE:
You DO NOT want this.
注意:你不想要这个。
// This globally assigned a new schema for me (for ALL models)
modelBuilder.HasDefaultSchema("security");
CONFIGURATION: OnModelCreating
This assigned a new schema for ONLY the mentioned tables
配置:onmodelcreate只为上述表分配了一个新的模式
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");
}
INITIAL MIGRATION LOOKS LIKE
最初的移民的样子
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");
}
}
#1
13
public class MyDbContext : EntityDbContext<ApplicationUser>
{
public DbSet<ApplicationUser> Users { get; set; }
public MyDbContext() : base()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// You can globally assign schema here
modelBuilder.HasDefaultSchema("schemaName");
}
}
#2
4
Here is a late entry explaining what I did. Not sure if there is a better way, but this is the ONLY thing that worked for me.
这里有一个很晚的条目解释我做了什么。不确定是否有更好的方法,但这是我唯一有效的方法。
To be fair, I have more than a single model in my context. Which is why this was better for me.
公平地说,在我的背景下,我有不止一个模型。这就是为什么这对我更好。
- Generate the tables in a database ahead of time (while tables are still in 'dbo')
- 提前在数据库中生成表(而表仍然在“dbo”中)
- Execute
add-migration
on your project and let it create a migration - 在项目上执行add-migration并让它创建一个迁移
- Change all the schemas within your migration code to the desired schema
- 将迁移代码中的所有模式更改为所需的模式
- Execute
update-database
to get those changes updated - 执行更新数据库以更新这些更改。
- Delete your original migration file (its' hash is useless to you)
- 删除原始迁移文件(它的散列对您没有用处)
- Execute
add-migration
again and let it create a new migration - 再次执行插件迁移,并让它创建一个新的迁移。
- Update the
OnModelCreating
method of your configuration with the code below - 使用下面的代码更新配置的onmodelcreation方法
- Run your application and start registering users
- 运行应用程序并开始注册用户
NOTE:
You DO NOT want this.
注意:你不想要这个。
// This globally assigned a new schema for me (for ALL models)
modelBuilder.HasDefaultSchema("security");
CONFIGURATION: OnModelCreating
This assigned a new schema for ONLY the mentioned tables
配置:onmodelcreate只为上述表分配了一个新的模式
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");
}
INITIAL MIGRATION LOOKS LIKE
最初的移民的样子
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");
}
}