EF核心外键配置问题

时间:2018-12-17 07:31:54

标签: ef-core-2.2

我正在将实际的Office项目迁移到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; }
}

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

我可以采取哪种方式?

感谢您的回复。

1 个答案:

答案 0 :(得分:1)

问题在GolfClubId的{​​{1}}中。您的PlanLang有清单 PlanPlanLang中的GolfClubId中有PlanLang,这是没有意义的,这就是EF Core无法配置/确定关系的原因。

GolfClubId中删除GolfClubId,因为PlanLang具有PlanLang的情况下,GlofClub已通过PlanPlan相关。 / p>

编写模型类,如下所示:

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

现在一切正常!

相关问题