[英]entity framework 6 code first change mapping from view to new table
我正在維護使用Entity Framework Code First進行遷移(不是自動)的WPF應用程序。 此應用程序中的某些POCO映射到視圖(指向另一個DB)。 我猜用於執行此操作的方法類似於此答案中的操作: 如何在代碼優先實體框架中使用視圖
現在,我希望其中一個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表。 什么是實現此目的的正確/好方法? 是否可以不自己編寫Up()和Down()方法?
當前,該應用程序使用Entity Framework 6.1,但是當首次創建此Company / vCompany映射時,我相信它是4.3版。
沒有足夠的信息/幫助來解決這個問題,但是我做了以下對我有用的事情(盡管很繁瑣):
我必須在遷移中完全重寫自動創建的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。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.