2

I am trying to generate a migration using code first approach. i generate the migration with the command :

add-migration -ConfigurationTypeName [FullyQualifiedConfigurationClass] [MigrationFile Name]

The Migration is generated successfully but it has code for previously deleted models from Models directory.

Here is a snippet of the migration code that shows table creation code for a model that i already deleted.

CreateTable(
                "dbo.PlaylistMovies",
                c => new
                    {
                        Playlist_Id = c.Int(nullable: false),
                        Movie_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.Playlist_Id, t.Movie_Id })
                .ForeignKey("dbo.Playlists", t => t.Playlist_Id, cascadeDelete: true)
                .ForeignKey("dbo.Movies", t => t.Movie_Id, cascadeDelete: true)
                .Index(t => t.Playlist_Id)
                .Index(t => t.Movie_Id);
);

What did I try and didn't work ?

  • changed the database context class name
  • changed the database name in context constructor
  • change the migration name over and over
Mina Gerges
  • 295
  • 2
  • 14
  • Migrations compare the prior migration model. Were those models in the prior migration and now removed? Show the code you are talking about. – Steve Greene May 01 '19 at 16:33
  • the question is edited – Mina Gerges May 01 '19 at 16:47
  • It is finding it somewhere in your code. Do a global search for PlaylistMovies. Check you Context for references, etc. – Steve Greene May 01 '19 at 16:55
  • 1
    if you work on a team, its possible someone else checked in a migration before you that had a generated datetime stamp of after yours. In this case, you'll see the above behavior. To fix it, you can generate a migration and comment out all code in the up/down methods (which will all be changes that a previous migration already accounts for), run it and then generate the migration for the changes youre trying to make. – GregH May 01 '19 at 17:04
  • @SteveGreene unfortunately i did search on the entire solution level and i did not find any references what so ever – Mina Gerges May 01 '19 at 17:39
  • @GregH i commented the code that is corresponding to the undesired table and executed the update-database command .. deleted the database and the migration .. re- added the migration still the table corresponding to the deleted model .. – Mina Gerges May 01 '19 at 17:44
  • 1
    @MinaGerges think you misunderstood my directions. if there is already a migration taht exists that accounts for your current unwanted changes, leave that migration alone. generate a NEW migration before you make any new model changes. comment out the code in that new migration, update-database, made your model changes, add your new migration, your new migration should only have the desired changes. there's no need to edit past migrations or delete your db or a migration or anything else in this case – GregH May 01 '19 at 17:52
  • 1
    That is a bridge table that EF creates automatically. So if you have a collection of Movies in your PlayList class or vice versa EF is going to need a table to make the many to many. See [here](https://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx) how EF creates the join table automatically. – Steve Greene May 01 '19 at 19:15
  • @SteveGreene that's exactly what it was .. when i deleted the model it creates it from it's own but when i put it back it creates the same one but with the name of PlaylistMovies1 .. now the problem is if i deleted the model i dont know how can i access the many to many table – Mina Gerges May 01 '19 at 20:04
  • lol .. i searched for it and bumped over your answer .. thanks :D Probably you should make an answer so i can give you the credit https://stackoverflow.com/a/35872730/9098126 – Mina Gerges May 01 '19 at 20:35
  • 1
    No problem. I believe EF Core requires an explicit table. – Steve Greene May 01 '19 at 20:46

0 Answers0