So I've run into weird situation today - while updating existing database with new tables I got extra column in one of my tables.
How it looked like:
Raport.cs
public class Raport { [DatabaseGenerated(DatabaseGeneratedOption.None)] [Key] public string raport_id { get; set; } //id raportu public string userID { get; set; } public DateTime creation_time { get; set; } public DateTime last_modyfication { get; set; } public virtual ICollection<ThrashType> ThrashType { get; set; } public virtual UserFrontInfo UserFrontInfo { get; set; }
}
ThrashType.cs
public class ThrashType { [Key] //[DatabaseGenerated(DatabaseGeneratedOption.None)] public int IdTrash { get; set; } public string RaportId { get; set; } public string ClassName { get; set; } public int Quantity { get; set; } public string Information { get; set; } public virtual Raport Raport { get; set; } }
UserFrontInfo.cs
public class UserFrontInfo { [DatabaseGenerated(DatabaseGeneratedOption.None)] [Key] public string userId { get; set; } public string userName { get; set; } public string whereFromName { get; set; } public virtual ICollection<Raport> Raport { get; set; } }
What I did before running into this situation: 1/added migrations 2/since I needed to append new tables I followed this: Reset Entity-Framework Migrations (1st comment under answers; more readable version) :
credits to Greg Gum for this (https://stackoverflow.com/users/425823/greg-gum)
The Issue: You have mucked up your migrations and you would like to reset it without deleting your existing tables. The Problem: You can't reset migrations with existing tables in the database as EF wants to create the tables from scratch. What to do: Delete existing migrations from Migrations_History table. Delete existing migrations from the Migrations Folder. Run add-migration Reset. This will create a migration in your Migration folder that includes creating the tables (but it will not run it so it will not error out.) You now need to create the initial row in the MigrationHistory table so EF has a snapshot of the current state. EF will do this if you apply a migration. However, you can't apply the migration that you just made as the tables already exist in your database. So go into the Migration and comment out all the code inside the "Up" method. Now run update-database. It will apply the Migration (while not actually changing the database) and create a snapshot row in MigrationHistory. You have now reset your migrations and may continue with normal migrations.
Then it all worked perfectly, but in ThrashType table I've got additional column named "Raport_raport_id".