PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 21, 2022

[FIXED] How to add EF Core Migrations to existing db when you have no __EFMigrationsHistory table?

 November 21, 2022     .net, c#, entity-framework, entity-framework-core, visual-studio     No comments   

Issue

I have an existing database in production which have not previously been involved with EF Migrations. As such, it is missing the __EFMigrationsHistory table.

I'm doing the following:

  • Invoking Add-Migration InitialCreate
  • Running the application
  • Stopping the application
  • Adding my modifications to the model
  • Invoking Update-Database
  • Getting the error Microsoft.Data.SqlClient.SqlException (0x80131904): There is already an object named 'sometablename' in the database.

I'm assuming this occurs because there is no migration history, and it tries to create everything from scratch.

How can I add migrations to a database which have not previously used it?


Solution

  1. If you are sure that your EF model matches the existing database's model then you can create the Migration History table manually and add row entries for the migrations which match the existing database models.
  2. If you have a large database it will be hard for you to confirm that the database model matches your new EF model. You will have to create a new database, run your migration on the new database and export data table by table from old to new.


Answered By - salman_sali
Answer Checked By - Marilyn (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing