我开始使用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) 我正在测试EF 4.3(测试版)
我有一些新的类应该生成db表和列.
从一个旧项目我的架构中有一些旧表,我想通过EF访问.声明所有类.为了访问旧表,有一个映射的poco.
db迁移也尝试创建旧表.
如何设置此类/表不是迁移的一部分,而是ef模型的一部分?
xxx.OnModelCreating()
{
modelBuilder.Ignore<myOldTableClass>();
}
Run Code Online (Sandbox Code Playgroud)
从模型中删除整个类.最后我无法通过dbContext使用它进行访问.
我喜欢使用自动迁移.我尽量避免将旧的db表完全迁移到EF类.(是的,我知道有生成器)有120个表,旧的应用程序仍然使用它们.
一些仅与EF(新应用程序)一起使用的新表.有3个常用的表格.那些不应该创建,但通过ef访问.