3

我一直在阅读有关 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");
    }
}

这是在表中生成两个外键,appointmentsSCHEDULEIDSchedule_Id1

我如何告诉 EF 不要创建Schedule_Id1

4

2 回答 2

6

试试这个:

HasRequired(a => a.Schedule).WithMany(x=> x.Appointment).Map(x => x.MapKey("SCHEDULEID"));

希望这有帮助。

于 2013-01-17T20:46:02.647 回答
1

您可以在属性 Appointments 中使用 InverseProperty 数据注释

public class Schedule
{
    public int Id { get; set; }
    [InverseProperty("Schedule")]
    public virtual ICollection<Appointment> Appointments { get; set; }
}

public class Appointment
{
    public int Id { get; set; }
    public Schedule Schedule { get; set; }
}
于 2013-01-18T06:49:34.890 回答