这需要分两步完成。首先,您需要强制 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 表包含到 pages_Membership 和 pages_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");
}
}