将SimpleMembershipProvider创建的表添加到迁移中

时间:2013-08-09 15:16:54

标签: asp.net-mvc entity-framework migration

我正在开发一个MVC 4应用程序,我在简单的成员资格提供程序(WebMatrix声明)的帮助下实现了一种身份验证机制。

当我运行应用程序时,会创建userProfiles,角色等的表。所以我接下来要做的是启用迁移。本质上,我想将这些现有表添加到DbMigration。

我想这样做的原因是运行update-database命令并提供一些种子数据(例如用户,角色)。

但是,当我运行Enable-Migrations时,它会创建一个DbMigration类,其中包含用于删除和创建除WebMatrix创建的表之外的所有表的命令。

有没有办法在迁移逻辑中包含所有表?

1 个答案:

答案 0 :(得分:3)

这需要分两步完成。 首先,您需要强制WebMatrix不创建表。该

InitializeDatabase方法有一个参数

至于创建表格。由于它们不是EF模型的一部分,因此Enable-Migrations不会创建它们。

您需要将表创建添加到初始迁移中。脚本可以在anwser

上找到
public partial class AddWebMatrixSecurityTables: DbMigration
{
    public override void Up()
    {
        Sql(@"Create ...");
    }

    public override void Down()
    {
        Sql("Drop....");
    }
}

如果您更喜欢使用Fluent API来构建表,则可以使用此代码。您必须修改它以将包含FK的UserProfile表包含到webpages_Membership和webpages_UsersInRoles

using System;
using System.Data.Entity.Migrations;

public partial class AddWebMatrixTables : DbMigration
{
    public override void Up()
    {
        CreateTable(
                "dbo.webpages_Membership",
                c => new
                         {
                                 UserId = c.Int(nullable: false, identity: true),
                                 CreateDate = c.DateTime(nullable: true),
                                 ConfirmationToken = c.String(nullable: true, maxLength: 128),
                                 IsConfirmed = c.Boolean(nullable: true, defaultValue: false),
                                 LastPasswordFailureDate = c.DateTime(nullable: true),
                                 PasswordFailuresSinceLastSuccess = c.Int(nullable: false, defaultValue: 0),
                                 Password = c.String(nullable: false, maxLength: 128),
                                 PasswordChangedDate = c.DateTime(nullable: true),
                                 PasswordSalt = c.String(nullable: false, maxLength: 128),
                                 PasswordVerificationToken = c.String(nullable: true, maxLength: 128),
                                 PasswordVerificationTokenExpirationDate = c.DateTime(nullable: true)
                         })
                .PrimaryKey(t => t.UserId);

        CreateTable(
                "dbo.webpages_OAuthMembership",
                c => new
                         {
                                 Provider = c.String(nullable: false, maxLength: 30),
                                 ProviderUserId = c.String(nullable: false, maxLength: 100),
                                 UserId = c.Int(nullable: false)
                         })
                .PrimaryKey(t => new {t.Provider, t.ProviderUserId});

        CreateTable(
                "dbo.webpages_Roles",
                c => new
                         {
                                 RoleId = c.Int(nullable: false, identity: true),
                                 RoleName = c.String(nullable: false, maxLength: 256)
                         })
                .PrimaryKey(t => t.RoleId);
        CreateTable(
                "dbo.webpages_UsersInRoles",
                c => new
                         {
                                 UserId = c.Int(nullable: false),
                                 RoleId = c.Int(nullable: false)
                         })
                .PrimaryKey(t => new {t.UserId, t.RoleId})
                .ForeignKey("dbo.webpages_Roles", t => t.RoleId);
    }

    public override void Down()
    {
        DropForeignKey("dbo.webpages_UsersInRoles", "RoleId", "dbo.webpages_Roles");
        DropForeignKey("dbo.webpages_UsersInRoles", "UserId", "dbo.webpages_Membership");
        DropTable("dbo.webpages_UsersInRoles");
        DropTable("dbo.webpages_Roles");
        DropTable("dbo.webpages_OAuthMembership");
        DropTable("dbo.webpages_Membership");
    }
}