1

I'm working on an MVC 4 application where i implement a authentication mechanism with the aid of the simple membership provider (WebMatrix assebly).

When i run the application the tables for userProfiles, roles etc. are created. So the next thing i'd like to do is to enable migrations. In essence, i want to add those existing tables to a DbMigration.

The reason i want to do that is to run the update-database command and provide some seed data (e.g. users, roles).

However, when i run the Enable-Migrations it creates a DbMigration class with commands for droping and creating all the tables apart from those that are created by the WebMatrix.

Is there any way to include all the tables within the migration logic?

ppoliani
  • 4,792
  • 3
  • 34
  • 62

1 Answers1

3

This needs to be done in two steps. First you need to force WebMatrix to not create the tables. There is a parameter to the InitializeDatabase methode for that

As for creating the table. Since they aren't part of your EF model they won't be created by the Enable-Migrations.

You will need to add the table creation to your initial migration. Scripts are available on that anwser

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

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

If you prefer to use the Fluent API to build the tables, you can use this code. You will have to modify it to include your UserProfile table with FK to both webpages_Membership and 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");
    }
}
Community
  • 1
  • 1
Alexandre Rondeau
  • 2,667
  • 24
  • 31
  • I'd like to ask you one more relevant question. What do you reckon, which project should i enable migration to? I have a DataAccess project (repostiories and uof) the DataLayer and finally the WebUI project. – ppoliani Aug 12 '13 at 08:22
  • You don't enable migration for a project, you're enable migration for a DbContext. Personnally when I'm addind a migration my command looks like Add-Migration -Name $migrationName -ConfigurationTypeName "MyDbContextConfiguration" -ProjectName "My.DataMapper.Project" -StartUpProject "My.WebUI.Project" – Alexandre Rondeau Aug 12 '13 at 13:02