2016-02-17 6 views
8

के रूप में बहु कॉलम इंडेक्स के साथ बीज AddOrUpdate 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; } 
} 

वर्तमान में मैं AddOrUpdate चढ़ाई ग्रेड के Name पर आधारित है, लेकिन अब मैं एक बिंदु है जहां मैं नकली नाम डालने की आवश्यकता पर हूँ।

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

क्या मैं डेटा डेटा डालने पर बहु ​​कॉलम अनुक्रमणिका की तुलना करना संभव है?

+0

"क्या तुलना करना संभव है ..." से आपका क्या मतलब है? आप वास्तव में क्या करना चाहते हैं? क्या आप केवल कुछ संभावित बार-बार मान रखना चाहते हैं? – JotaBe

+1

यदि डेटा डेटाबेस पहले से मौजूद है, तो मैं संदर्भ के रूप में बहु कॉलम अनुक्रमणिका का उपयोग करना चाहता हूं। उदाहरण: '.AddOrUpdate (ग्रेड => ग्रेड.Name == मौजूदा। नाम और ग्रेड। ग्रेड टाइप == मौजूदा। ग्रेड टाइप)' – Silthus

उत्तर

7

आपको एकाधिक कॉलम निर्दिष्ट करने के लिए अनाम प्रकार का उपयोग करने की आवश्यकता है। यह भी इंडीज निर्दिष्ट किए बिना काम करता है।

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