Entity Framework: Add-Migration fails with Unable to update database

后端 未结 4 862
别那么骄傲
别那么骄傲 2021-02-03 11:24

I have been using Entity Framework (5.0) for a while now in a project (ASP.NET MVC in VS2012 Express). Right now, though, I am no longer able to add migrations.

         


        
相关标签:
4条回答
  • 2021-02-03 11:45

    You can reset the entity framework to solve your problem [But keep it mind it will bring the Migration to the default state]

    Note: To take a backup before performing the following

    You need to delete the present state:

    • Delete the migrations folder in your project
    • Delete the __MigrationHistory table in your database (may be under system tables)

    You will find the __MigrationHistory table in your database [Under App_Data Folder]

    Then run the following command in the Package Manager Console:

    Enable-Migrations -EnableAutomaticMigrations -Force
    

    Use with or without -EnableAutomaticMigrations

    And finally, you can run:

    Add-Migration Initial
    

    This may also help you

    0 讨论(0)
  • 2021-02-03 11:45

    This worked for me:

    update-database -targetmigration:"0" -force -verbose
    add-migration Initial
    update-database
    
    0 讨论(0)
  • 2021-02-03 11:52

    Never use automigrations, that gave me problems in the past (when migrating the database down, use the correct way to do it from the start!)

    This line should be in your global.asax:

    Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyContext, Configuration>());
    

    And not in your DbContext!

    Other tips if the above won't help:

    Perhaps you have multiple layers in your application:

    Add-Migration 000  -StartupProjectName "NameOfTheProjectInSolutionExplorer" -ConfigurationTypeName "MyConfiguration"  -ConnectionString "theconnectionstring;" -ConnectionProviderName "System.Data.SqlClient" -Verbose
    

    Above is the Add-Migration command i use for a multi-layered application.

    Same thing for an update of the database

    Update-Database -ConfigurationTypeName "SlaveConfiguration" -StartupProjectName "FacturatieMVCv2.Data" -Verbose -script
    
    0 讨论(0)
  • 2021-02-03 11:56

    In my case I've got the same error because I was forcing ObjectContext.CommandTimeout on class DbContext at constructor method during migration.

    Try removing it

    ((IObjectContextAdapter)this).ObjectContext.CommandTimeout = 5000;
    
    0 讨论(0)
提交回复
热议问题