我有一个遗留表,我需要将我的应用程序连接到。我使用的是代码优先的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