0

Say I have an app with this Entity Framework model:

public class Author
{
    public int ID { get; set; }
    public string Name{ get; set; }
}

public class Context : DbContext
{
    public DbSet<Author> Authors{ get; set; }
}

So I do the usual stuff where I run the app, and the initial db schema is created, then enable migrations and update the database.

Now, say I add another property to Author, string Bio. I type Add-Migration AddBio and the appropriate files are generated.

Here's where things getconfusing: I've tried comment everything inside the Up method of the newly created migration (which is just 1 line for adding a new column, Bio) and do Update-Database. Now, when I try to add a new author to my database, I get "error occurred, see inner exception for details" message from the runtime.

I realize that this is as a consequence of inconsistencies between the last model snapshot and the database schema (correct me if I'm wrong). The moment I do Add-Migration ... a new model snapshot is created, and it assumes that Description is part of the database schema...it's just that it's not.

Is there a way to fix things if I arrive at this sort of situation (I assume this is a very easy way to deliberately confuse someone who's working on a .NET project)? Roll back to the previous migration and then uncomment the code of the latest migration and run it again? Add a (new) migration that will contain the commented out content? Thanks.

anemaria20
  • 1,646
  • 2
  • 17
  • 36

2 Answers2

0

If I understood you correctly - you can't really do things like that 'ad hoc', as that's not going to end well. Unless it's all about experimenting - or you really understand all the intricacies involved.

Basically you have '3 parts' involved in the equation:

  • your code, POCO classes
  • the Db structure, tables etc. that are generated based on your model
  • and the __MigrationHistory table that holds the 'metadata' and synchronizes the two sort of

what you did is to comment the part that generates necessary fields in the Db - but your model/classes are still the same (so you should comment things in there as well, not that I recommend doing it).

Bottom line, things need to be and stay in sync.

The best way to do that is to avoid any manual changes, either in the Db or in the generated code - and follow best practices rules, i.e. follow what's recommended.

Here are some links (of my answers) describing some more ways to get into trouble and get yourself out of it if / when it happens:
how to manually sync with the Db
How to delete and recreate from scratch an existing EF Code first database

Community
  • 1
  • 1
NSGaga-mostly-inactive
  • 14,052
  • 3
  • 41
  • 51
0

If you want to rollback your latest migration you can try to complete these steps

1) Firstly execute Get-Migrations command in 'Package Manager Console' you will receive migrations that have been applied to your database. for example: 201605181535083_ThisOneWhichYouWantRollback 201605181535083_LastSuccessful 201605181535083_Initial

2) Execute update-database -TargetMigration "LastSuccessful" command. This command will rewind and undo any migrations between the current and the “LastSuccessful” migration.

3) After that you can try to uncomment your migration code and Update-Database one more time.

References: How to Rollback Entity Framework Code First Migration

Vlad
  • 211
  • 2
  • 10
  • Ideally your answer would contain the bulk of the relevant details; link-only answers are considered low quality, as the link can change/go stale/dead over time. This leaves future readers without an effective answer. – Castaglia Jun 05 '16 at 22:42