Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot apply migration
    primarykey
    data
    text
    <p>I am trying to apply a migration to my database.</p> <p>Initially the DB was reverse-engineered to code first using the EF power-tools. in my database the table names all begin with tbl_[NAME] so after reverse engineering process completed I all the Models had this same convention. Since the model name is mapped to table name in the modelMAP for each model, I decided to rename all the generated models to remove the "tbl_" when I created the migration and had a look at the "UP" method, I could see that that EF wants to DropForeignKey on all affected tables, all the tables, then drop the tables!!! WHAT?</p> <p>Since my DB is large I will show only a few lines of code in the migration UP method.</p> <pre><code>DropForeignKey("dbo.tbl_AssetFileLocations", "AssetFileID", "dbo.tbl_AssetFiles"); DropForeignKey("dbo.tbl_AssetFileLocations", "StorageLocationID", "dbo.tbl_StorageLocations"); ... DROP ALL FOREIGN KEYS ON ALL TABLES ... DropIndex("dbo.tbl_AssetFileLocations", new[] { "AssetFileID" }); DropIndex("dbo.tbl_AssetFileLocations", new[] { "StorageLocationID" }); ... DROP ALL INDEXES ... CreateTable( "dbo.tbl_AssetFileLocations", c =&gt; new { AssetLocationID = c.Guid(nullable: false), AssetFileID = c.Guid(nullable: false), StorageLocationID = c.Guid(nullable: false), DateCreatedonStorage = c.DateTime(nullable: false), }) .PrimaryKey(t =&gt; t.AssetLocationID) .ForeignKey("dbo.tbl_AssetFiles", t =&gt; t.AssetFileID, cascadeDelete: true) .ForeignKey("dbo.tbl_StorageLocations", t =&gt; t.StorageLocationID, cascadeDelete: true) .Index(t =&gt; t.AssetFileID) .Index(t =&gt; t.StorageLocationID); ... CREATE ALL OTHER TABLES ... DropTable("dbo.tbl_AssetFileLocations"); ... DROP ALL TABLES ... </code></pre> <p>I have no data in the database so though I am not sure why EF has decided to drop all tables and recreate them it's not a problem in my experiment. However I would like to understand why changing the model names cause a full database rebuild (the new name maps to the same table name as when the DB was reverse engineered!)</p> <p><strong>My main issue</strong></p> <p>when attempting to apply the migration I end up in a circular error storm.</p> <p><strong>1st attempt gives error:</strong> FK_dbo.tbl_AssetFileLocations_dbo.tbl_AssetFiles_AssetFileID' is not a constraint. Could not drop constraint. See previous errors.</p> <p>So I think ok, I will comment out the DropForeignKey //DropForeignKey("dbo.tbl_AssetFileLocations", "AssetFileID", "dbo.tbl_AssetFiles");</p> <p>then it errors on the next dropForeignKey with the same (contextually) error. So as an experiment I comment out all the DropForeignKey statements.</p> <p>re-running the update-database command then results in the error message:</p> <p>Cannot drop the index 'dbo.tbl_AssetFileLocations.IX_AssetFileID', because it does not exist or you do not have permission.</p> <p>and one by one it complains about all DropIndex statements. So As an experiment I comment out all the and try to update-database</p> <p>So as we can see the next thing the UP method attempts to do is Add all the tables! Of course I get the error: There is already an object named 'tbl_AssetFileLocations' in the database.</p> <p>So I figure well EF your being a bit dull here, you are trying to drop the tables after you create them! so I change the order of the Drop table commands so that they come before the Create Table commands because that makes more sense. </p> <p>Since this is just an experiment with EF code first and migration it is all cool. I am happy for EF to delete the entire DB schema and rebuild it.</p> <p>So with the Drop Table commands now in the correct place, I re-run the Update-Database to get the final circular error:</p> <p>Could not drop object 'dbo.tbl_Assets' because it is referenced by a FOREIGN KEY constraint.</p> <p>And to myself I think "YES, that's the FOREIGN KEY constraint, you refused to drop at the beginning of this exercise.</p> <p>So where do I go from here. From this experiment it would appear that Code-First migrations don't work for all but the simple examples given on the EF website!</p> <p>Has anyone reverse engineered a large DB and been successful at making code changes and migrating those to the DB?</p> <p>Should I forget code-first and return to Database first (I'd rather not - I love the idea of code first)?</p> <p>Please help EF people.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload