Reputation: 18805
I am having a strange issue where my EF Automatic Migrations are removing all of the data within from all tables whenever I make a change to any one of the Entity's.
I've seen this similar question however for the life of me I can't derive from the links what I am missing in my own implementation.
Note that the application works as completely fine, with data being retrieved and saved to the mysql database via EF - it's just a great annoyance to have to re-create all the data everytime I want modify an Entity.
I am using EF6.0 and a MySql database.
Context.cs
namespace Dock.Models
{
[DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))]
public class DockContext : DbContext
{
public DockContext(): base("name=DockContext")
{
Database.SetInitializer(new DockInitializer());
}
public virtual DbSet<Profile> Profiles { get; set; }
public virtual DbSet<Dock> Docks { get; set; }
public virtual DbSet<Crate> Crates { get; set; }
public virtual DbSet<Subscription> Subscriptions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
}
}
}
Confirguration.cs
internal sealed class Configuration : DbMigrationsConfiguration<DockContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = false;
SetSqlGenerator("MySql.Data.MySqlClient", new MySqlMigrationSqlGenerator());
CodeGenerator = new MySqlMigrationCodeGenerator();
}
protected override void Seed(DockContext context)
{
Profile admin = new Profile {
NotAPrimaryId = 168879070,
DisplayName = "Admin101"
};
context.Profiles.AddOrUpdate(a => a.NotAPrimaryId , admin);
context.SaveChanges();
}
}
And an example of one of the Entity's Dock.cs
namespace Dock.Entities
{
public class Dock
{
[Key]
public int DockId { get; set; }
public int ProfileId { get; set; }
public int GameId { get; set; }
public string GameData { get; set; }
public bool Active { get; set; }
// Navigational Properties
[ForeignKey("CrateId")]
public virtual List<Crate> Crates { get; set; } = new List<Crate>();
}
}
Upvotes: 1
Views: 104
Reputation: 150108
EF will create migration classes for you. You don't have to write them by hand.
Automatic migrations can cause problems in my experience when different team members commit updates at different times.
I've also had to hand-edit the generated code-based migrations on several occasions, something that can't be done with automatic migrations.
I suggest switching to manual migrations so that you have complete visibility into and control of the migration process.
Upvotes: 1