我正在维护一个使用Entity Framework Code First和迁移(不是自动)的WPF应用程序。此应用程序中的某些POCO映射到视图(指向另一个DB)。我想用于执行此操作的方法与此答案中的操作类似:how to use views in code first entity framework
现在我希望其中一个POCO(公司)指向一个表而不是一个视图。自从指向视图以来,POCO也发生了一些变化。
当我在POCO和CompanyConfiguration类(更改ToTable())中进行更改后添加迁移时,似乎迁移认为视图是现有表并尝试重命名它。例如,Up() - 方法的开头如下所示:
RenameTable(name: "dbo.vCompany", newName: "Company");
AlterColumn("dbo.Company", "ParentAccount", c => c.String(maxLength: 160));
AlterColumn("dbo.Company", "Country", c => c.String(maxLength: 100));
但是,我不希望更改视图,我希望从头开始创建与POCO匹配的Company-table。实现这一目标的正确/好方法是什么?是不是可以自己编写Up()和Down()方法?
目前,该应用程序使用Entity Framework 6.1,但是当首次创建此Company / vCompany映射时,我认为它是版本4.3。
答案 0 :(得分:1)
没有太多信息/帮助来实现这个目标,但我做了以下对我有用的工作(尽管工作非常繁琐):
我必须在迁移中完全重写自动创建的Up()和Down()方法。使用CreateTable()创建表而不是重命名视图。
在Up()方法中:
CreateTable(
"dbo.Company",
c => new
{
CompanyId = c.Guid(nullable:false),
ParentAccount = c.String(maxLength:160),
Country = c.String(maxLength:100),
Address1_City = c.String(maxLength:4000),
Address1_Country = c.String(maxLength: 4000),
Address1_Line1 = c.String(maxLength: 4000),
Address1_Line2 = c.String(maxLength: 4000),
Address1_PostalCode = c.String(maxLength:50),
Owner = c.String(maxLength:160),
OwnerId = c.Guid(nullable:false),
Name = c.String(maxLength:160),
EMailAddress = c.String(maxLength:100),
InvoiceEMailAddress = c.String(maxLength:100),
Fax = c.String(maxLength:50),
CreditLimit = c.Decimal(storeType:"money"),
CreditOnHold = c.Boolean(nullable:false, defaultValue:false),
IsPrivate = c.Boolean(nullable:false, defaultValue:false),
StatusCode = c.Int(),
CustomerTypeCode = c.Int(),
BEGreenCreditLimit = c.Int(),
IssuingBodyAccount = c.String(maxLength:20),
CustomerType = c.String(maxLength:100),
IsElProducer = c.Boolean(nullable:false, defaultValue:false),
IsEnergyTrader = c.Boolean(nullable:false,defaultValue:false),
VATNumber = c.String(maxLength:100),
ContactPerson = c.String(maxLength:160),
ContactPersonPhone = c.String(maxLength:50),
ContactPersonFax = c.String(maxLength:50),
ContactPersonEmail = c.String(maxLength:100)
}
)
.PrimaryKey(p=>p.CompanyId)
;
从另一个数据库中提取的公司值,为某些列引用long maxLength。