Daniel
Daniel

Reputation: 3364

"update-database" Entity framework code first doesn't add tables anymore

I'm using Entity Frame work 5 code first in my website. Every thing was fine and I've been using

update-database -verbose

to update database with new data or new structure and model, however; out of no where update-database has stopped working and it doesn't track changes in model and new tables anymore.

I have searched over internet and some say that I have to delete database context and recreate it to make it work. I can't do this, the project is kind of big and more than 4k lines would be affected if I delete the database context.

I'm using visual studio 2012 with the 3rd update, so update is not the solution either.

any suggestions.

Upvotes: 3

Views: 12909

Answers (4)

From your Migration file which is created right after the 'Add-Migration 'migrationname' ' command. Check the changes that it has listed. Keep that much and remove the other things. For example, it could be recreating all your tables in the 'Up' method and deleting tables in the 'Down' method. Remove all the unwanted changes and save the file. Then do an 'update-database' command in the PM Console.

Upvotes: 0

Tone Bone
Tone Bone

Reputation: 41

I'm no expert by any means but my tables weren't created with the PM> Update-Database command so I:

  1. Deleted the database (SQL Server)
  2. Deleted the Migrations folder (Root/Migrations)
  3. Re-added the database manually with a right click on the "Databases" folder in SQL Server Object Explorer in VS2019; Insure that the name of your database is the exact name of the database name in your "appsettings.json" for example ("sqlConnection": "server=[MyServerName]; database=[DB NAME]; Integrated Security=true")
  4. The run "Add-Migration InitialMigration" and "Update-Database" in VS2019 Package Manager

And your tables and columns will appear. This worked for me. Again I am a newb just figuring stuff out.

Upvotes: 4

Gautam Ganguly
Gautam Ganguly

Reputation: 21

Sometimes they dont generate as they dont get registerd correctly in the IdentityModels.cs file.

Just after the context creation method

public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }

Add the class registration

public System.Data.Entity.DbSet<PROJECT.Models.MODELNAME> MODELNAME { get; set; }

Build the project and run update-database -verbose (the -verbose switch will show the updates made)

Upvotes: 2

Daniel
Daniel

Reputation: 3364

as I told you deleting the database context didn't fix my problem. This is how I fixed the issue. I dropped table __MigrationHistory using sql command and run the update-database -verbose again.

Apparently something was wrong with this automatic created table.

Upvotes: 5

Related Questions