Automatic Migrations for ASP.NET SimpleMembershipProvider
Asked Answered
D

3

43

So I tried to use automatic migrations with my new MVC 4 Project but somehow it isn't working. I followed this blog post step by step.

I've added the changes to the UserProfile account model (the NotaryCode field):

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }
    public string UserName { get; set; }
    public int NotaryCode { get; set; }
}

Then I wrote on the package manager console enable-migrations and a Configuration class appeared (inherits from DbMigrationsConfiguration<Web.Models.UsersContext>) then I fill the class as:

public Configuration()
{
    AutomaticMigrationsEnabled = true;
}

protected override void Seed(Atomic.Vesper.Cloud.Web.Models.UsersContext context)
{
    WebSecurity.InitializeDatabaseConnection(
            "DefaultConnection",
            "UserProfile",
            "UserId",
            "UserName", autoCreateTables: true);

    if (!Roles.RoleExists("Atomic"))
        Roles.CreateRole("Atomic");

    if (!Roles.RoleExists("Protocolista"))
        Roles.CreateRole("Protocolista");

    if (!Roles.RoleExists("Cliente"))
        Roles.CreateRole("Cliente");

    string adminUser = "randolf";

    if (!WebSecurity.UserExists(adminUser))
        WebSecurity.CreateUserAndAccount(
            adminUser,
            "12345",
            new { NotaryCode = -1 });

    if (!Roles.GetRolesForUser(adminUser).Contains("Atomic"))
        Roles.AddUsersToRoles(new[] { adminUser }, new[] { "Atomic" });
}

And then I tried to run update-database -verbose but this doesn't work. I mean, this is the output:

There is already an object named 'UserProfile' in the database.

PM> update-database -verbose
Using StartUp project 'Web'.
Using NuGet project 'Web'.
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Target database is: 'VesperCloud' (DataSource: .\SQLSERVER, Provider: System.Data.SqlClient, Origin: Configuration).
No pending code-based migrations.
Applying automatic migration: 201211051825098_AutomaticMigration.
CREATE TABLE [dbo].[UserProfile] (
    [UserId] [int] NOT NULL IDENTITY,
    [UserName] [nvarchar](max),
    [NotaryCode] [int] NOT NULL,
    CONSTRAINT [PK_dbo.UserProfile] PRIMARY KEY ([UserId])
)
System.Data.SqlClient.SqlException (0x80131904): There is already an object named 'UserProfile' in the database.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
   at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
ClientConnectionId:a7da0ddb-bccf-490f-bc1e-ecd2eb4eab04
**There is already an object named 'UserProfile' in the database.**

I know the object exists. I mean, I'm try to use automatic-migrations to, precisely, modify and run without recreating manually the DB. But somehow this isn't working.

I look the MSDN documentation and found the property:

AutomaticMigrationDataLossAllowed = true;

But setting it to true doesn't change anything. I guess I'm missing something but somehow doesn't find what. Any idea?

Dwightdwindle answered 5/11, 2012 at 18:36 Comment(1)
Please look at my answer on #26305773 Hope this helps...Amusing
N
127

update-database -verbose doesn't work because your model has been changed after your data table already existed.

First, make sure there are no changes to the UserProfile class. Then, run:

Add-Migration InitialMigrations -IgnoreChanges

This should generate a blank "InitialMigration" file. Now, add any desired changes to the UserProfile class. Once changes are added, run the update command again:

update-database -verbose

Now the automatic migration will be applied and the table will be altered with your changes.

Neoarsphenamine answered 9/11, 2012 at 8:56 Comment(5)
Actually this worked perfectly. Unfortunately I can't give you more than a +1 and the accepted answer. You save me tons of headaches. Thanks again. Next thing is, should I always use the Add-Migration InitialMigrations? Or this this is just for the first time. I don't find any online doc about it.Dwightdwindle
You're welcome. You just need to run it only once to create one InitialMigrations class.Neoarsphenamine
You solved my 2-day problem with a simple option "-IgnoreChanges". I should be able to click UpVote 100000 times.Eastnortheast
If I'm getting There is already an object named 'AspNetRoles' in the database. what changes do I add to the blank InitialMigration file?Coronel
This worked like a charm. Although I don't understand why I needed to create Initial migration file using Add-Migration InitialMigrations -IgnoreChanges, when I dropped my database and re-created it. Can anyone explain? I'm able to only update database if I add the empty Initial Migrations file.Presbytery
D
6

What it looks like happened here is that you enabled migrations, then ran the application. By running the application before using the UpdateDatabase command, EntityFramework would have created and populated the database but since when you enabled migrations the database didn't exist, it didn't create the InitialCreate migration. Migrations still thinks that you have an empty database and wants to create all of the objects in your model

What you can try is to either re-enable migrations which will generate an InitialCreate migration that reflects the current state of the database. In this case I would save the changes you made to the seed method than run "Enable-Migrations -Force", this should recreate the migration and generate an IntialCreate migration. You can then repopulate your seed method and run the UpdateDatabase command.

Decidua answered 7/11, 2012 at 19:38 Comment(0)
P
0

I had same and sorted in different way. Went to my local db deleted the UserProfile and other tables having foreign key constraints webpages_Membership,webpages_OAuthMembership,webpages_Roles,webpages_UsersInRoles tables. All these will recreate when you run update-database -verbose.

Psalms answered 20/9, 2015 at 10:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.