我正在努力将实际的办公项目迁移到实体框架核心和.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)。
我可以通过哪种方式进行?
感谢您的回复。
问题出在PlanLang
的GolfClubId
。您的Plan
有列表PlanLang
和GolfClubId
,其中单个PlanLang
具有GolfClubId
没有意义,这就是 EF Core 无法配置/确定关系的原因。
从PlanLang
中删除GolfClubId
,因为PlanLang
已经通过Plan
与GlofClub
相关,以防Plan
GolfClubId
。
按如下所示编写模型类:
public class GolfClub
{
public int Id { get; set; }
//.. other properties, not important
}
public class Plan
{
public int PlanId { get; set; }
public int GolfClubId { get; set; }
//.. other properties, not important
public GolfClub GolfClub { get; set; }
public ICollection<PlanLang> PanLangs { get; set; }
}
public class PlanLang
{
public int PlanId { get; set; }
public string Lang { get; set; }
//.. other properties, not important
public Plan Plan { get; set; }
}
并按如下方式重写Plan
和PlanLang
EntityConfigurations
:
public class PlanConfiguration : IEntityTypeConfiguration<Plan>
{
public void Configure(EntityTypeBuilder<Plan> builder)
{
builder.ToTable("gc_plan_master");
builder.HasKey(k => k.PlanId);
builder.HasMany(p => p.PlanLangs).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.PlanId, k.Lang });
builder.Property(p => p.Lang).HasMaxLength(5);
builder.HasOne(p => p.Plan).WithMany().HasForeignKey(FK => FK.PlanId);
}
}
现在一切应该正常了!