0

当您尝试使用复合键创建一对多关系时,我收到错误“序列包含多个匹配元素”

请帮帮我!

modelBuilder.Entity<PracticePilotScoringInfo>()
                    .HasKey(info => new { info.DriverName, info.Control,info.VehicleClass, info.ScoringInfoId });

        modelBuilder.Entity<PracticeScoringInfo>()
            .HasKey(info => info.Id)
            .HasMany(info => info.PracticePilotScoringInfos)
            .WithRequired(info => info.ScoringInfo)
            .HasForeignKey(info => new { info.DriverName, info.Control, info.VehicleClass, info.ScoringInfoId });



        public class PracticeScoringInfo : ScoringInfo
{
    public int GrandPrixId { get; set; }
    public GrandPrix GrandPrix { get; set; }

    public virtual ICollection<PracticePilotScoringInfo> PracticePilotScoringInfos { get; set; }
}


public class PracticePilotScoringInfo : PilotScoringInfo
{
    public string DriverName { get; set; }
    public ControlType Control { get; set; }
    public string VehicleClass { get; set; }
    public Guid ScoringInfoId { get; set; }
    public virtual PracticeScoringInfo ScoringInfo { get; set; }
}


        public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;

    }
4

1 回答 1

0

看起来我在 Fluent API 映射中没有被识别出来。下面的声明工作正常

            modelBuilder.Entity<ScoringInfo>()
            .HasKey(info => info.Id);

        modelBuilder.Entity<PracticeScoringInfo>()
            .HasKey(info => info.Id);

        modelBuilder.Entity<PilotScoringInfo>()
                    .HasKey(info => new { info.DriverName, info.Control, info.VehicleClass, info.ScoringInfoId });

        modelBuilder.Entity<PracticePilotScoringInfo>()
            .HasKey(info => new { info.DriverName, info.Control, info.VehicleClass, info.ScoringInfoId });
于 2013-09-01T14:36:41.533 回答