我有兩個簡單的類使用實體框架7,除了一種情況下,實體框架生成與已有列相同的名稱的新備用鍵,所有類都可以正常工作。實體框架7創建一個不需要的替代鍵
Invoice.cs
namespace Arinsys.Accounts
{
[Table(name: "Invoice", Schema = "Arinsys_Accounts")]
public class Invoice
{
public long ID { get; set; }
public string InvoiceID { get; set; }
public long? CustomerUID { get; set; }
public long? EmployeeUID { get; set; }
public DateTime BillDateTime { get; set; }
public long Amount { get; set; }
public long? Discount { get; set; }
public string DiscountReason { get; set; }
public string CouponID { get; set; }
public long Charges { get; set; }
public string ChargesReason { get; set; }
public long Tax { get; set; }
public string TaxType { get; set; }
public long FinalAmount { get; set; }
public string Status { get; set; }
public string Remarks { get; set; }
[ForeignKey("CustomerUID")]
public virtual CRM.User Customer { get; set; }
[ForeignKey("EmployeeUID")]
public virtual CRM.User BilledBy { get; set; }
public virtual ICollection<InvoiceItems> Items { get; set; }
public virtual ICollection<Payments> Payments { get; set; }
}
}
生成的發票表格代碼
CREATE TABLE [Arinsys_Accounts].[Invoice] (
[ID] BIGINT IDENTITY (1, 1) NOT NULL,
[Amount] BIGINT NOT NULL,
[BillDateTime] DATETIME2 (7) NOT NULL,
[Charges] BIGINT NOT NULL,
[ChargesReason] NVARCHAR (MAX) NULL,
[CouponID] NVARCHAR (MAX) NULL,
[CustomerUID] BIGINT NULL,
[Discount] BIGINT NULL,
[DiscountReason] NVARCHAR (MAX) NULL,
[EmployeeUID] BIGINT NULL,
[FinalAmount] BIGINT NOT NULL,
[InvoiceID] NVARCHAR (MAX) NOT NULL,
[InvoiceID1] NVARCHAR (450) NOT NULL,
[Remarks] NVARCHAR (MAX) NULL,
[Status] NVARCHAR (MAX) NULL,
[Tax] BIGINT NOT NULL,
[TaxType] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_Invoice] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [AK_Invoice_InvoiceID1] UNIQUE NONCLUSTERED ([InvoiceID1] ASC),
CONSTRAINT [FK_Invoice_User_CustomerUID] FOREIGN KEY ([CustomerUID]) REFERENCES [Arinsys_CRM].[User] ([ID]),
CONSTRAINT [FK_Invoice_User_EmployeeUID] FOREIGN KEY ([EmployeeUID]) REFERENCES [Arinsys_CRM].[User] ([ID])
);
InvoiceItems.cs
namespace Arinsys.Accounts
{
[Table(name: "InvoiceItems",Schema = "Arinsys_Accounts")]
public class InvoiceItems
{
public long ID { get; set; }
public string InvoiceID { get; set; }
public string ItemID { get; set; }
public int Quantity { get; set; }
public int Price { get; set; }
public int Discount { get; set; }
public string Reason { get; set; }
public int Amount { get; set; }
public string Remarks { get; set; }
[ForeignKey("InvoiceID")]
public virtual Invoice Invoice { get; set; }
}
}
生成InvoiceItems表的代碼
CREATE TABLE [Arinsys_Accounts].[InvoiceItems] (
[ID] BIGINT IDENTITY (1, 1) NOT NULL,
[Amount] INT NOT NULL,
[Discount] INT NOT NULL,
[InvoiceID] NVARCHAR (450) NULL,
[ItemID] NVARCHAR (MAX) NULL,
[Price] INT NOT NULL,
[Quantity] INT NOT NULL,
[Reason] NVARCHAR (MAX) NULL,
[Remarks] NVARCHAR (MAX) NULL,
CONSTRAINT [PK_InvoiceItems] PRIMARY KEY CLUSTERED ([ID] ASC),
CONSTRAINT [FK_InvoiceItems_Invoice_InvoiceID] FOREIGN KEY ([InvoiceID]) REFERENCES [Arinsys_Accounts].[Invoice] ([InvoiceID1])
);
注意在發票表InvoiceID1欄,它會自動創建的,我不需要它。由於這些模型都在庫中,它們就像模板一樣,實際的dbcontext將在應用程序中,我不希望最終開發人員擔心這些事情,所以 OnModelCreating中的流暢API不是一種選擇。關於下一個顯而易見的問題,我們如何管理引用,總體結構和接口的設計是如何處理的。無論如何,討論將超出當前問題的範圍。我可以在InvoiceItems表中看到外鍵設置爲InvoiceID1,但我想要它鏈接到發票表中的ID列,如果我添加一個[InverseProperty(「ID」)]屬性以及現有的[ ForeignKey(「InvoiceID」)]屬性InvoiceItems類中的Invoice對象出現錯誤
ArgumentNullException:值不能爲空。參數名:類型
在編寫實體框架的時候還是蠻新的文件還沒有完成,並#2等地所有的答案告訴答案,如何做到這一點,即如何建立一個備用/獨特的鑰匙,但不幸的是我找不到任何答案如何防止它。
編輯:雖然這個問題是我的部分編程錯誤,由@JCL回答,但我們收到了一些很好的答案,例如@Steve Greene在EF SQL中發佈了關於字符串長度限制的問題,這可能對未來的讀者有所幫助,所以我將問題保持原樣
請發佈您的InvoiceItems類。 – jvanrhyn
「模特在圖書館」是什麼意思?爲什麼阻止你使用Fluent API? –
他正在使用EF Core 1.0。文檔可能不如EF6 – jvanrhyn