ASP.Net标识 - 使用自定义架构

mar*_*tin 15 asp.net entity-framework-6 asp.net-identity

我首先使用MVC5 + Ef6代码与ASP.Net Identity 1.0,并希望在自定义架构中创建表.即不是dbo架构的架构.

我使用Ef power tools对我的数据库进行了反转,并将映射类中所有其他表的模式名称设置为以下内容

this.ToTable("tableName", "schemaName");
Run Code Online (Sandbox Code Playgroud)

我尝试为ASP.Net表做这个,但它一直给我很多错误,最终我放弃了.如果我从我的项目中排除(逆向工程)ASP.Net Identity表,它们将被创建,但始终在dbo模式中

有人知道怎么做吗?

Bra*_*tie 17

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");
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是否会更改此上下文中可能出现的所有 DBSet 的默认架构?有没有办法只为“ApplicationUser”做到这一点? (2认同)

Pri*_*ERO 5

这是一个迟到的条目,解释我做了什么.不确定是否有更好的方法,但这是唯一对我有用的东西.

公平地说,在我的背景下,我有不止一个模型.这就是为什么这对我来说更好.

  1. 提前在数据库中生成表格(而表格仍在"dbo"中)
  2. 执行add-migration您的项目并让它创建一个迁移
  3. 将迁移代码中的所有模式更改为所需的模式
  4. 执行update-database以更新这些更改
  5. 删除原始迁移文件(其哈希对您没用)
  6. add-migration再次执行并让它创建一个新的迁移
  7. OnModelCreating使用以下代码更新配置方法
  8. 运行您的应用程序并开始注册用户

注意:
你不想要这个.

// This globally assigned a new schema for me (for ALL models)
modelBuilder.HasDefaultSchema("security");
Run Code Online (Sandbox Code Playgroud)

配置: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");
}
Run Code Online (Sandbox Code Playgroud)

初始移民看起来像

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");
    }
}
Run Code Online (Sandbox Code Playgroud)