【发布时间】:2022-01-08 21:54:29
【问题描述】:
我有一名员工,有一份按小时计酬的工作,每个小时有多个考勤卡。我希望将考勤卡链接到员工和每小时。
public class Employee
{
public int Id { get; set; }
}
public class Hourly
{
public int EmployeeId { get; set; }
public List<Timecard> Timecards{ get; set; }
}
public class Hourly
{
public int HourlyId{ get; set; }
public int EmployeeId { get; set; }
}
如何在 EF 中指定这种关系。
代码似乎设置了employeeID,但导致迁移出现问题,并且Hourly 现在设置为null。
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Timecard>()
.HasOne<HourlyPC>()
.WithMany(pc => pc.Timecards)
.HasForeignKey(t => t.EmployeeId)
.HasPrincipalKey(pc => pc.EmployeeId);
}
【问题讨论】:
标签: entity-framework