我有一個遺留表,我需要將我的應用程序連接到。我使用的是代碼優先的POCO模型。我有以下類:代碼與實體框架的第一個關係,流利的API
public class Equipment
{
[Key]
public string EquipmentId { get; set; }
public string OriginatorId { get; set; }
public virtual Employee Employee { get; set; }
}
public class Employee
{
[Key]
[Column("employee_id")]
public string EmployeeId { get; set; }
public string EmployeeName { get; set; }
[ForeignKey("OriginatorEmployeeId")]
public virtual Equipment Equipment { get; set; }
}
我需要在Employee類映射到僱員在設備類OriginatorEmployeeId。
此外,遺留表由Employee類表示。該表實際上被命名爲employee(小寫),EmployeeId列被命名爲「employee_id」。我想保持我的類和屬性的名稱與應用程序的其餘部分一致,因此Employee和EmployeeId一致。
下面是我用流利的API嘗試:
modelBuilder.Entity<Employee>().Map(m =>
{
m.MapInheritedProperties();
m.ToTable("employee");
});
modelBuilder.Entity<Equipment>()
.HasOptional<Employee>(u => u.Employee)
.WithOptionalDependent(c => c.Equipment).Map(p => p.MapKey("OriginatorEmployeeId"));
我可能混合的東西我不需要。我現在得到的錯誤是:
Multiplicity is not valid in Role 'Equipment_Employee_Source' in relationship 'Equipment_Employee'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.
任何幫助表示讚賞。
您OnModelCreating事件我不熟悉實體框架,但應該將外鍵映射到像EquipmentId這樣的真實ID而不是對象設備? – PmanAce
謝謝PmanAce。通過實體框架(EF),Employee類中的Equipment對象是導航屬性。這是EF代表關係的方式 – steveareeno
你試圖達到什麼樣的關係? – Pluc