我正在将实际的办公项目迁移到 Entity Framework Core 和 .NET Core。当我想做添加迁移时,我被卡住了。似乎导航属性和外键配置不正确。经过多次尝试,我无法解决我的问题。
添加迁移时的错误消息:
The relationship from 'PlanLang.Plan' to 'Plan.Lang' with foreign key properties {'PlanId' : int} cannot target the primary key {'GolfClubId' : int, 'PlanId' : int} because it is not compatible. Configure a principal key or a set of compatible foreign key properties for this relationship.
这是我的实体:
高尔夫俱乐部
public class GolfClub
{
public int Id { get; set; }
//.. other properties, not important
}
计划
public class Plan
{
public int GolfClubId { get; set; }
public int PlanId { get; set; }
//.. other properties, not important
public GolfClub GolfClub { get; set; }
public ICollection<PlanLang> Lang { get; set; }
}
计划朗
public class PlanLang
{
public int GolfClubId { get;set; }
public int PlanId { get; set; }
public string Lang { get; set; }
//.. other properties, not important
public GolfClub GolfClub { get; set; }
public Plan Plan { get; set; }
}
这是我的配置:
public class GolfClubConfiguration : IEntityTypeConfiguration<GolfClub>
{
public void Configure(EntityTypeBuilder<GolfClub> builder)
{
builder.ToTable("gc_master");
builder.HasKey(k => new { k.Id });
builder.Property(p => p.Id).ValueGeneratedNever(); // to disable auto-increment
}
}
public class PlanConfiguration : IEntityTypeConfiguration<Plan>
{
public void Configure(EntityTypeBuilder<Plan> builder)
{
builder.ToTable("gc_plan_master");
builder.HasKey(k => new { k.GolfClubId, k.PlanId });
builder.Property(p => p.GolfClubId).ValueGeneratedNever(); // to disable auto-increment
builder.HasMany(p => p.Lang).WithOne(p => p.Plan).HasForeignKey(FK => FK.PlanId);
builder.HasOne(p => p.GolfClub).WithMany().HasForeignKey(FK => FK.GolfClubId);
}
}
public class PlanLangConfiguration : IEntityTypeConfiguration<PlanLang>
{
public void Configure(EntityTypeBuilder<PlanLang> builder)
{
builder.ToTable("gc_plan_master_lang");
builder.HasKey(k => new { k.GolfClubId, k.PlanId, k.Lang });
builder.Property(p => p.GolfClubId).ValueGeneratedNever(); // to disable auto-increment
builder.Property(p => p.Lang).HasMaxLength(5);
builder.HasOne(p => p.GolfClub).WithMany().HasForeignKey(FK => FK.GolfClubId);
}
}
我想要的是 Plan 有 1 个外键(GolfClubId)和 PlanLang 2 个外键(PlanId 和 GolfClubId)。
我可以通过哪种方式继续这样做?
谢谢你的回复。