stuck in EF migration limbo

后端 未结 7 1508
慢半拍i
慢半拍i 2020-12-20 10:58

i have somehow gotten my EF5 project into a state where I can\'t proceed.

When I do an \'update-database\' i get:

Unable to update database to

相关标签:
7条回答
  • 2020-12-20 11:37

    This can happen when you are trying to merge with migration which has the same base as your migration because of which there is model difference in one of migration schema even though that table exists

    To solve this problem what you can do is create merge migration by command

    Add-Migration -IgnoreChanges

    followed by migration name

    This creates an empty migration with the current model as a snapshot. Thus this will solve your model difference problem and your tables and models will be in sync

    0 讨论(0)
  • 2020-12-20 11:38

    For me the issue was that we had renamed the namespace of the migration 2014123456_Initial.cs.
    But VS had not regenerated the namespace in its associated 2014123456_Initial.Designer.cs.

    Once the Designer.cs was changed to use the same namespace, it all started working again.


    (also posted this as an answer here because both questions are so similar)

    0 讨论(0)
  • 2020-12-20 11:40

    I changed the following value in my Configuration.cs class from false to true.

    AutomaticMigrationsEnabled = true; 
    

    In App_Data folder I renamed my project's database - the file with .mdf ending (so a new one will be created), and in Package Manager Console I entered the following command:

    update-database

    after which the pending migrations ran smoothly.

    Note this worked for me, but I'm not 100% if this is the best practice. In any case this Entity Framework Code First guide for migrations says:

    "If you get an error that indicates a table already exists and can't be created, it is probably because you ran the application after you deleted the database and before you executed update-database. In that case, delete the Movies.mdf file again and retry the update-database command. If you still get an error, delete the migrations folder.."

    Also about AutomaticMigrationsEnabled = true; this MSDN article, Data Points : A Code First Migrations Mystery: Solved tells "Migrations can run automatically, meaning that model changes will be discovered and migrations corresponding to changes will be created and executed on the database. All of this happens at run time during database initialization. Automatic migrations are handy for simple apps, but you have very little control over them and I typically don’t recommend enabling them. I was happy when Code First switched the default to false."

    0 讨论(0)
  • 2020-12-20 11:44

    I did mistake in creation database

        using System.ComponentModel.DataAnnotations;
        using System.Globalization;
        namespace ProductsManager.Models
        {
            public class Product
            {
                public int ProductId { get; set; }
                public string Name { get; set; }
                public string Description { get; set; }
                public string Production { get; set; }
                public string Size { get; set; }
                public decimal<--- Price { get; set; }
                public string Barcode { get; set; }
            }
        }
    

    after add-migration Initial I realized and changed code to public int Price { get; set; } did same add-migration DummyMigration and its created in migration folder 080372472_dummyMigration

    namespace IdetityBeta.Migrations
    {
        using System;
        using System.Data.Entity.Migrations;
    
        public partial class DummyMigration : DbMigration
        {
            public override void Up()
            {
                AlterColumn("dbo.Products", "Price", c => c.String());
            }
    
            public override void Down()
            {
                AlterColumn("dbo.Products", "Price", c => c.Decimal(nullable:             false, precision:              18, scale: 2));
            }
        }
    }
    

    So then update-database and problem was solved

    0 讨论(0)
  • 2020-12-20 11:46

    What worked for me was:

    1. Reverting all my changes (making a backup first) back to a known good state.
    2. Doing add-migration DummyMigration. That produced some spurious changes that I commented
    3. Called update-database to add the migration + metadata to the [__MigrationHistory] table.
    4. Make the changes I needed (from the backup of the code I took earlier).
    5. Do the normal add-migration/update-database.

    Not ideal, and would be cool to see if there's a better solution, but that worked for me.

    0 讨论(0)
  • 2020-12-20 11:49
    1. Update-Database –TargetMigration <second_last_migration>
    2. Add-Migration <full_name_including_timestamp_of_last_migration>

      You need to include the timestamp so that migrations knows you want to edit the existing migration rather than scaffolding a new one. This will update the metadata for the last migration to match the current model.

    3. Update-Database

    Source https://docs.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict

    0 讨论(0)
提交回复
热议问题