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

时间:2014-02-03 13:58:12

标签: asp.net entity-framework-6 asp.net-identity

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

我使用Ef电动工具反转设计我的数据库,并将映射类中所有其他表的模式名称设置为以下

this.ToTable("tableName", "schemaName");

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

任何人都知道怎么做?

3 个答案:

答案 0 :(得分:16)

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 :(得分:5)

这是一个迟到的条目,解释了我的所作所为。不确定是否有更好的方法,但这是唯一对我有用的东西。

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

  1. 提前在数据库中生成表格(而表格仍然在&#39; dbo&#39;)
  2. 在您的项目上执行add-migration并让它创建迁移
  3. 将迁移代码中的所有模式更改为所需的模式
  4. 执行update-database以更新这些更改
  5. 删除原始迁移文件(其对您来说无用)
  6. 再次执行add-migration并让它创建新的迁移
  7. 使用以下代码更新配置的OnModelCreating方法
  8. 运行您的应用程序并开始注册用户
  9. 注意:
    你不想要这个。

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

答案 2 :(得分:0)

对不起,我用的是谷歌翻译器。

Prisioner ZERO指示的某些步骤不是必需的。 提供的指示基于具有个人用户帐户安全性的标准模板

首先,我们必须验证我们的项目是否干净(在Package Management Console中插入命令):

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

现在,我们只需要生成迁移即可。为此,请在Package Management Console中输入以下命令(您可以选择使用-OutputDir参数指示输出路径):

Add-Migration InitialSchemaIdentity -OutputDir Data\Migrations

然后,使用以下命令将更改应用于数据库:

Update-Database