我的项目中有一个模型如下:
public class Model
{
public int Id { get; set; }
public long FromNo { get; set; }
public long ToNo { get; set; }
public string Content { get; set; }
public long TicketNo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
迁移如下
public override void Down()
{
AlterColumn("dbo.Received", "FromNo", c => c.Long(nullable: false));
AlterColumn("dbo.Received", "ToNo", c => c.Long(nullable: false));
AlterColumn("dbo.Received", "TicketNo", c => c.Long(nullable: false));
}
public override void Up()
{
AlterColumn("dbo.Received", "FromNo", c => c.String());
AlterColumn("dbo.Received", "ToNo", c => …Run Code Online (Sandbox Code Playgroud) c# entity-framework visual-studio ef-migrations entity-framework-5
我开始使用Entity Framework 4.3 Code First进行手动迁移和SQL Express 2008并最近更新到EF5(在VS 2010中),并注意到现在当我更改类似外键约束时,迁移代码会添加"dbo".到表名的开头,因此它构造的外键名称对于现有约束是不正确的(现在通常看起来很奇怪).
EF 4.3中的原始迁移脚本(注意ForeignKey("Products",t => t.Product_Id)):
CreateTable(
"Products",
c => new
{
Id = c.Int(nullable: false, identity: true),
ProductName = c.String(),
})
.PrimaryKey(t => t.Id);
CreateTable(
"KitComponents",
c => new
{
Id = c.Int(nullable: false, identity: true),
Component_Id = c.Int(),
Product_Id = c.Int(),
})
.PrimaryKey(t => t.Id)
.ForeignKey("Products", t => t.Component_Id)
.ForeignKey("Products", t => t.Product_Id)
.Index(t => t.Component_Id)
.Index(t => t.Product_Id);
Run Code Online (Sandbox Code Playgroud)
生成的外键名称:FK_KitComponents_Products_Product_Id FK_KitComponents_Products_Component_Id
如果我然后升级到EF5并更改外键,则迁移代码看起来像(注意"dbo.KitComponents"和"dbo.Products"而不是"KitComponents"和"Products"):
DropForeignKey("dbo.KitComponents", "Product_Id", "dbo.Products");
DropIndex("dbo.KitComponents", …Run Code Online (Sandbox Code Playgroud)