gpt4 book ai didi

c# - 同一张表的 EF 代码第一个重复外键

转载 作者:行者123 更新时间:2023-11-30 14:13:22 26 4
gpt4 key购买 nike

我一直在阅读有关 EF Code First 生成重复外键的 SO 帖子,并尝试将解决方案应用于我的代码但无法修复我的代码。

这是我的类(class)

 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 表中生成两个外键,即 SCHEDULEIDSchedule_Id1

如何告诉 EF 不要创建 Schedule_Id1

最佳答案

试试这个:

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

希望这对您有所帮助。

关于c# - 同一张表的 EF 代码第一个重复外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14387597/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com