Csharpfunbag
Csharpfunbag

Reputation: 425

Entity Framework migration history not recognized after Identity 2.0 upgrade?

I am in the process of upgrading from Simple Membership to Identity 2.0 in my existing web project. Some of the bigger changes that involve entity framework is the DBContext inheritance and UserProfiles changing to Users.

I use PMC to add migrations and update the database instead of allowing automatic migrations at run time.

The expectation is that after my code changes for getting to Identity 2.0 that I would add a migration and it would generate the schema changes into my migrations folder.

For some reason it is no longer recognizing the existing migration history table in the current db! When I run add-migration it says there are existing migrations that aren't applied and that I should run update-database first. I run update-database -script to see what it was trying to do and it wants to create the migration history table and each migration in my project.

It seems that somehow the migration history table is no longer recognized. I ran get-migrations to confirm it was talking to the correct database and it was but no migrations have been applied.

Running Entity Framework 6.1.1

Any suggestions?

 internal sealed class Configuration : DbMigrationsConfiguration<Namespace.MyContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = false;
    }

    protected override void Seed(Namespace.MyContext context)
    {
         //Stuff to initialize an empty db
    }


 public class UserProfile : IdentityUser<int, CustomIdentityUserLogin, CustomIdentityUserRole, CustomIdentityUserClaim>, IUser<int>
{
    [Required]
    public string Name { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<UserProfile, int> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
    //other properties related to the user profile

}

Edit 2:

As a test, I deleted the migrationhistory table, ran "update-database -targetmigration mostcurrentmigraiton -script", then deleted out all the changes except for the migration history from the script, ran the script. I then tried to get-migrations and still "no migrations have been applied to the target database" appeared. I verified that it did create the table and the product version had updated for previous migrations where they were older. Please help!

Upvotes: 1

Views: 2462

Answers (2)

Csharpfunbag
Csharpfunbag

Reputation: 425

After much troubleshooting I found the problem to be one that must be security related. Get-Migrations will return the message I have seen when no password is entered in the connection string that the dbcontext is using. Interestingly enough, using localdb for development, I had the correct username and password as the update-database was able to create new tables but some level of access must not be available for it to get the migrations. What I found most strange is that get-migrations didn't say there was a credential issue!

Upvotes: 1

Yoda
Yoda

Reputation: 18068

I wanted to add comment but it has gone too long.

First option: try update-database -force in PCM.

Second option: if this is not already deployed web application and you don't care about data loss yet, my answer here can help: Second attempt to generate script sql script did not work

Third option: There is one thing more that comes to my mind. If you create new ASP .NET MVC 5.1 with template including Identity 2.0 you get class IdentityModels.cs in Models. It looks like this:

namespace WebApplication2.Models {
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser {

        PROPERTIES OF APPLICATION USER HERE


        public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager) {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser> {


        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false) {
            System.Diagnostics.Debug.WriteLine("CONSTRUCTOR");
            Configuration.LazyLoadingEnabled = true;
            Configuration.ProxyCreationEnabled = true;
        }
        //stuff in database
        public DbSet<Person> Persons { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder) {
            base.OnModelCreating(modelBuilder);
        }

        public static ApplicationDbContext Create() {

            return new ApplicationDbContext();

        }


    }
}

and then in Application_Start() in Global.asax you have:

   protected void Application_Start() {


            Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, Configuration>());
            new ApplicationDbContext().Database.Initialize(true);
  ...more stuff
  }

and your code might lack these two lines from Application_Start but with your DbContext(I used ApplicationDbContext here).

If this didn't help, sorry, I am out of options.

Upvotes: 1

Related Questions