2

我正在将实际的办公项目迁移到 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)。

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

谢谢你的回复。

4

1 回答 1

1

问题GolfClubId出在PlanLang. 您Plan拥有的列表PlanLangGolfClubId个人PlanLang拥有的列表GolfClubId没有意义,这就是 EF Core 无法配置/确定关系的原因。

Remove GolfClubIdfrom PlanLangbecause PlanLangis already related to GlofClubthrough Planbecase Planhas 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并将and改写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);
    }
}

现在一切都应该正常了!

于 2018-12-17T08:01:17.243 回答