加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 同一个表的EF Code First Duplicate Foreign Key

发布时间:2020-12-16 01:52:34 所属栏目:百科 来源:网络整理
导读:我一直在阅读有关EF Code First生成重复外键的SO帖子,并尝试将解决方案应用于我的代码但无法修复我的代码. 这是我的课程 public class Schedule{ public int Id { get; set; } public ICollectionAppointment Appointments { get; set; }}public class Appoi
我一直在阅读有关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");
    }
}

这在约会表中生成两个外键,即SCHEDULEID和Schedule_Id1.

如何告诉EF不要创建Schedule_Id1

解决方法

试试这个:

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

希望这有帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读