I was at the Spike branch playing with EF Migrations, and as soon as I got what I wanted, I created the actual dev branch from scratch (based on the previous branch, which does not have any DB or ORM code at all).
New code
I have a context that inherits from a DbContext using this DbSet:
public DbSet<Warehouse> Warehouses { get; set; }
And the warehouse contains the following:
public class Warehouse
{
public string Code { get; set; }
public string Name { get; set; }
}
Migration
If I run
add-migration Warehouse
I get a file with a name 201708040819010_Warehouse.csthat contains the following:
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");
}
This is not at all what I expected.
What's wrong
All of these renaming, changing column columns and columns seem to suggest that somehow Migrations is convinced that it needs to update an existing table in an existing db.
, localdb, .
Warehouse ( ), Id, Name Abbreviation.
. .
, ,
Visual Studio Migrations , . , , , .
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)
{
}
}
, () ?