No initial create with Entity Framework migrations

后端 未结 6 1856
梦如初夏
梦如初夏 2021-01-30 09:13

I\'m trying to get Entity framework migrations working. I\'ve enabled code first migrations, its created a migrations folder, config file and the mig history table, but no initi

6条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 09:55

    I am using EF 6 RC1 and ran into this problem where neither the InitialCreate nor __MigrationHistory were being created when running Enable-Migrations.

    Actually, just after upgrading from EF 5 to EF 6 I ran Enable-Migrations and for some reason it created a __MigrationHistory table using the EF 5 schema, so I deleted it and my Migrations directory and tried to start over.

    But every time I deleted the Migrations directory it wouldn't create an InitialCreate or __MigrationHistory. I tried dropping and recreating the database and restarting Visual Studio 2012 to no avail. I gave up for the day and the next morning tried again - after letting my computer sit for about 8 hours it then created the InitialCreate. I am guessing there must be a cache somewhere that has a really long timeout - anyone? I am also guessing that rebooting might clear the cache, but I didn't try that.

    Whatever the case, it is possible to use PM> Add-Migration InitialCreate to do that step manually.

    Anyway, I still didn't get a __MigrationHistory table. Apparently, EF 6 has changed from creating it during the Enable-Migrations command to instead only creating it during the Update-Database command. And since my schema had already been created at that point, I needed to tear it down and recreate it manually:

    PM> Update-Database -TargetMigration:0
    PM> Update-Database
    

    I also stopped after the first command to check the state of the database to ensure I was updating the correct one, since according to this, the database connection string is picked up or autogenerated depending on the configuration, and unless it is configured right, there is no guarantee you are going to access the database or instance of SQL Server you intend to.

    After running both commands it created a __MigrationHistory table - and it didn't create it as a system table (which I didn't really want anyway), so all is good. Not exactly the same problem as the OP, but hopefully this will be helpful to someone else.

    References:

    • http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/
    • Reset Entity-Framework Migrations
    • How to recreate migrations from scratch

提交回复
热议问题