我一直在阅读有关 EF Code First 生成重复外键的 SO 帖子,并尝试将解决方案应用于我的代码但无法修复我的代码。
这是我的课
public class Schedule
{
public int Id { get; set; }
public ICollection<Appointment> Appointments { get; set; }
}
public class Appointment
{
public int Id { get; set; }
public Schedule Schedule { get; set; }
}
public class ScheduleConfiguration : EntityTypeConfiguration<Schedule>
{
public ScheduleConfiguration()
{
HasKey(s => s.Id);
Property(s => s.Id).HasColumnName("SCHEDULEID");
ToTable("SCHEDULES");
}
}
public class AppointmentConfiguration : EntityTypeConfiguration<Appointment>
{
public AppointmentConfiguration()
{
HasKey(a => a.Id);
Property(a => a.Id).HasColumnName("APPOINTMENTID");
HasRequired(a => a.Schedule).WithMany().Map(x => x.MapKey("SCHEDULEID"));
ToTable("APPOINTMENTS");
}
}
这是在表中生成两个外键,appointments
即SCHEDULEID
和Schedule_Id1
。
我如何告诉 EF 不要创建Schedule_Id1