Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架-使用多列索引作为标识符的种子添加或更新_C#_Entity Framework_Ef Code First_Entity Framework Migrations_Seeding - Fatal编程技术网

C# 实体框架-使用多列索引作为标识符的种子添加或更新

C# 实体框架-使用多列索引作为标识符的种子添加或更新,c#,entity-framework,ef-code-first,entity-framework-migrations,seeding,C#,Entity Framework,Ef Code First,Entity Framework Migrations,Seeding,我试图使用context.AddOrUpdate方法为数据库种子,但问题是我需要基于多列索引使插入的数据唯一 [Table("climbing_grades")] public class ClimbingGrade : EntityBase { /// <summary> /// The name of the climbing grade, e.g.: 7a, VII, etc. /// </summary> [Index("IX_Na

我试图使用
context.AddOrUpdate
方法为数据库种子,但问题是我需要基于多列索引使插入的数据唯一

[Table("climbing_grades")]
public class ClimbingGrade : EntityBase
{
    /// <summary>
    /// The name of the climbing grade, e.g.: 7a, VII, etc.
    /// </summary>
    [Index("IX_Name_GradeType", 1, IsUnique = true)]
    public string Name { get; set; }

    /// <summary>
    /// Tries to display the average difficulty of the described grade.
    /// Matching the different grades can be difficult because its always
    /// a subjective rating and there exists no norm on converting grades.
    /// </summary>
    public double Difficulty { get; set; }

    /// <summary>
    /// The type of the grade. Will be the respective region rating.
    /// e.g.: UUIA for most oft europe, YSD for USA, etc.
    /// </summary>
    [Index("IX_Name_GradeType", 2, IsUnique = true)]
    public ClimbingGradeType GradeType { get; set; }
}

插入种子数据时是否可以比较多列索引?

您需要使用匿名类型来指定多列。这也可以在不指定标记的情况下工作

context.ClimbingGrades.AddOrUpdate(grade => new { grade.Name, grade.GradeType },
    new ClimbingGrade
    {
        Name = "5a",
        Difficulty = 4.75,
        GradeType = ClimbingGradeType.FontainebleauBloc
    },
    new ClimbingGrade
    {
        Name = "5a",
        Difficulty = 4.25,
        GradeType = ClimbingGradeType.FontainebleauTraverse
    });

你所说的“有可能比较…?”是什么意思?你到底想做什么?是否只保留一些可能重复的值?如果数据库中已经存在数据集,我希望使用多列索引作为引用。e、 g.:
.AddOrUpdate(grade=>grade.Name==existing.Name&&grade.GradeType==existing.GradeType)
context.ClimbingGrades.AddOrUpdate(grade => new { grade.Name, grade.GradeType },
    new ClimbingGrade
    {
        Name = "5a",
        Difficulty = 4.75,
        GradeType = ClimbingGradeType.FontainebleauBloc
    },
    new ClimbingGrade
    {
        Name = "5a",
        Difficulty = 4.25,
        GradeType = ClimbingGradeType.FontainebleauTraverse
    });