I was on a Spike branch, playing around with EF Migrations and once I got what I wanted, created the actual dev branch from scratch (based on a previous branch that does not have any DB or ORM related code at all).
The new code
I have a context that inherits from DbContext with this DbSet:
public DbSet<Warehouse> Warehouses { get; set; }
And Warehouse contains this:
public class Warehouse
{
public string Code { get; set; }
public string Name { get; set; }
}
Migrations
If I run
add-migration Warehouse
I get a file called 201708040819010_Warehouse.cs
, which contains this:
public override void Up()
{
RenameTable(name: "dbo.Warehouses", newName: "Warehouse");
DropPrimaryKey("dbo.Warehouse");
AddColumn("dbo.Warehouse", "Code", c => c.String(nullable: false, maxLength: 128));
AlterColumn("dbo.Warehouse", "Name", c => c.String(maxLength: 100));
AddPrimaryKey("dbo.Warehouse", "Code");
CreateIndex("dbo.Warehouse", "Name", unique: true);
DropColumn("dbo.Warehouse", "Id");
DropColumn("dbo.Warehouse", "Abbreviation");
DropTable("dbo.People");
}
Which is not at all what I expected.
What's wrong
All those renames, alter column and drop columns seem to suggest that somehow Migrations is convinced that it needs to update an existing table in an existing db.
But that's not true, because I removed my old localdb, am working on a completely new branch and on a new migration.
Not completely out of the blue
I did however have a Warehouse class (when playing around in that Spike branch) that contained an Id, Name and Abbreviation.
But that's old news. And doesn't physically exist anymore.
What I suspect is going on
Is that Visual Studio is tripping over itself and is basing the new Migrations on information that is stored in it's temporary folders. I did a clean of what looked like it could be related based on this post, but to no positive effect.
Update
DbContext
public class MyDbContext : DbContext, IMyDbContext
{
public MyDbContext()
:base("MyDb")
{
Configuration.LazyLoadingEnabled = false;
Database.SetInitializer(new NullDatabaseInitializer<MyDbContext>());
}
public DbSet<Warehouse> Warehouses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations
.AddFromAssembly(Assembly.GetExecutingAssembly());
modelBuilder.Conventions
.AddFromAssembly(Assembly.GetExecutingAssembly());
}
}
EF Migrations config
internal sealed class Configuration : DbMigrationsConfiguration<MyDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(MyDbContext context)
{
}
}
Question
What can I do to fix this (weird) behaviour?