Sequence包含多个匹配元素 - 复合键

时间:2013-09-01 11:44:57

标签: entity-framework-5

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

请帮帮我!

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;

    }

1 个答案:

答案 0 :(得分:0)

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

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