EF Core外键配置问题

EF Core Foreign Key configuration problem

我正在将实际的办公项目迁移到 Entity Framework Core 和 .NET Core。 当我想进行添加迁移时,我被卡住了。似乎 Navigation 属性 和 Foreign Key 配置不正确。经过多次尝试,我无法解决我的问题。

添加迁移时的错误消息:

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; }
}

PlanLang

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)。

我可以通过哪种方式继续这样做?

感谢您的回复。

问题出在 PlanLangGolfClubId 中。您的 Plan 有 List PlanLangGolfClubId 其中个人 PlanLangGolfClubId 这没有意义,这就是为什么 EF Core 不能 configuring/determining 关系。

PlanLang 中删除 GolfClubId 因为 PlanLang 已经通过 PlanGlofClub 相关,因为 PlanGolfClubId .

写下你的模型类如下:

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; }
}

并重写PlanPlanLangEntityConfigurations如下:

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);
    }
}

现在一切正常!