Basically if during Enable-Migrations we detect that db exists, we create initial migration. If this database had stored procedure, we scaffold proc with empty body. This seems fine (if we cant detect what is the actual body f stored procedure in database). However, on down operation, we drop that stored procedure. We should not do that, because we did not create it, and essentially can't re-create it. We should either produce a stored proc with an actual body (if we have means to do so), or *not* drop stored proc that we have not created.
Repro - run the following code (via Initializer) then use Enable-Migrations
```
using System.Data.Entity;
using System.Linq;
namespace MigrationShananigans
{
public class BlackHole
{
public int Id { get; set; }
public decimal Mass { get; set; }
}
public class BlackHoleContext : DbContext
{
public BlackHoleContext()
{
Database.SetInitializer(new DropCreateDatabaseAlways<BlackHoleContext>());
}
public DbSet<BlackHole> BlackHoles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<BlackHole>().MapToStoredProcedures();
}
}
class Program
{
static void Main(string[] args)
{
using (var ctx = new BlackHoleContext())
{
ctx.BlackHoles.ToList();
}
}
}
}
```
Initial migration that gets created looks as follows:
```
public override void Up()
{
CreateTable(
"dbo.BlackHoles",
c => new
{
Id = c.Int(nullable: false, identity: true),
Mass = c.Decimal(nullable: false, precision: 18, scale: 2),
})
.PrimaryKey(t => t.Id);
CreateStoredProcedure(
"dbo.BlackHole_Insert",
p => new
{
Mass = p.Decimal(precision: 18, scale: 2),
},
body: ""
);
CreateStoredProcedure(
"dbo.BlackHole_Update",
p => new
{
Id = p.Int(),
Mass = p.Decimal(precision: 18, scale: 2),
},
body: ""
);
CreateStoredProcedure(
"dbo.BlackHole_Delete",
p => new
{
Id = p.Int(),
},
body: ""
);
}
public override void Down()
{
DropStoredProcedure("dbo.BlackHole_Delete");
DropStoredProcedure("dbo.BlackHole_Update");
DropStoredProcedure("dbo.BlackHole_Insert");
DropTable("dbo.BlackHoles");
}
```
Repro - run the following code (via Initializer) then use Enable-Migrations
```
using System.Data.Entity;
using System.Linq;
namespace MigrationShananigans
{
public class BlackHole
{
public int Id { get; set; }
public decimal Mass { get; set; }
}
public class BlackHoleContext : DbContext
{
public BlackHoleContext()
{
Database.SetInitializer(new DropCreateDatabaseAlways<BlackHoleContext>());
}
public DbSet<BlackHole> BlackHoles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<BlackHole>().MapToStoredProcedures();
}
}
class Program
{
static void Main(string[] args)
{
using (var ctx = new BlackHoleContext())
{
ctx.BlackHoles.ToList();
}
}
}
}
```
Initial migration that gets created looks as follows:
```
public override void Up()
{
CreateTable(
"dbo.BlackHoles",
c => new
{
Id = c.Int(nullable: false, identity: true),
Mass = c.Decimal(nullable: false, precision: 18, scale: 2),
})
.PrimaryKey(t => t.Id);
CreateStoredProcedure(
"dbo.BlackHole_Insert",
p => new
{
Mass = p.Decimal(precision: 18, scale: 2),
},
body: ""
);
CreateStoredProcedure(
"dbo.BlackHole_Update",
p => new
{
Id = p.Int(),
Mass = p.Decimal(precision: 18, scale: 2),
},
body: ""
);
CreateStoredProcedure(
"dbo.BlackHole_Delete",
p => new
{
Id = p.Int(),
},
body: ""
);
}
public override void Down()
{
DropStoredProcedure("dbo.BlackHole_Delete");
DropStoredProcedure("dbo.BlackHole_Update");
DropStoredProcedure("dbo.BlackHole_Insert");
DropTable("dbo.BlackHoles");
}
```