簡體   English   中英

ASP.Net 標識 - 使用自定義架構

[英]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");
    }
}

這是一個解釋我做了什么的遲到條目。 不確定是否有更好的方法,但這是唯一對我有用的方法。

公平地說,在我的上下文中,我有不止一個模型。 這就是為什么這對我更好。

  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");

配置: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");
    }
}

對不起,我的英語,我使用谷歌翻譯。

零號囚徒指示的某些步驟不是必需的。 提供的指示基於具有個人用戶帳戶安全性的標准模板

首先,我們必須驗證我們的項目是否干凈(在包管理控制台中插入命令):

  1. 如果您已經使用默認 ASP.NET Identity 架構創建了一個數據庫,則必須使用以下命令刪除該數據庫(或直接在 SQL Server 中刪除):

Drop-Database

  1. 如果您有 ASP.NET Identity 模板的默認遷移,請執行以下命令將其刪除:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM