2016-02-17 18 views
8

Próbuję zasiać bazę danych przy użyciu metody context.AddOrUpdate, ale problemem jest to, że muszę wstawić wstawione dane unikalne na podstawie indeksu wielu kolumn.Entity Framework - Seed AddOrUpdate z indeksem wielu kolumn jako identyfikatorem

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

Obecnie AddOrUpdate na podstawie Name w klasie wspinaczki, ale teraz jestem w punkcie, gdzie Muszę wstawić zduplikowanych nazw.

context.ClimbingGrades.AddOrUpdate(grade => /* Compare multi column index here?*/, 
    new ClimbingGrade 
    { 
     Name = "5a", 
     Difficulty = 4.75, 
     GradeType = ClimbingGradeType.FontainebleauBloc 
    }, 
    new ClimbingGrade 
    { 
     Name = "5a", 
     Difficulty = 4.25, 
     GradeType = ClimbingGradeType.FontainebleauTraverse 
    }); 

Czy możliwe jest porównanie indeksów wielu kolumn podczas wstawiania danych nasion?

+0

Co masz na myśli przez "czy można porównać ...?" Co dokładnie chcesz robić? Czy chcesz zachować tylko niektóre z możliwych powtarzających się wartości? – JotaBe

+1

Chcę użyć indeksu wielu kolumn jako odwołania, jeśli zestaw danych już istnieje w bazie danych. np .: '.AddOrUpdate (grade => grade.Name == existing.Name && grade.GradeType == existing.GradeType)' – Silthus

Odpowiedz

7

Aby określić wiele kolumn, należy użyć typu anonimowego. Działa to również bez określania wskazań.

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 
    }); 
Powiązane problemy