2016-11-08 4 views
5

मुझे पता था कि जब आप कक्षाओं के बीच कई संबंध रखते हैं तो उलटा संपत्ति का उपयोग किया जाता है। लेकिन मैं व्यस्त संपत्ति और विदेशी कुंजी संपत्ति के बीच उलझन में हूं क्योंकि दोनों का उपयोग संबंधों को परिभाषित करने के लिए किया जाता है।इकाई ढांचे में व्यस्त संपत्ति और विदेशी कुंजी के बीच क्या अंतर है?

public class PrivilegeToDbOperationTypeMap : BaseEntity 
{ 
    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Column(Order = 0)] 
    public int PrivilegeToDbOperationTypeMapId { get; set; } 

    [ForeignKey("privilegeLookup"), Column(Order = 1)] 
    [Index("IX_PrivilegeLookupId_DbOperationLookupId", 1, IsUnique = true)] 
    public int PrivilegeLookupId { get; set; } 

    [ForeignKey("dbOperationTypeLookup"), Column(Order = 2)] 
    [Index("IX_PrivilegeLookupId_DbOperationLookupId", 2, IsUnique = true)] 
    public int DbOperationLookupId { get; set; } 

    #region Navigation Properties 

    public PrivilegeLookup privilegeLookup { get; set; } 

    public DbOperationTypeLookup dbOperationTypeLookup { get; set; } 

    [InverseProperty("privilegeToDbOperationTypeMap")] 
    public ICollection<RoleToPrivilegeDbOperationTypeMap> roleToPrivilegeDbOperationTypeMaps { get; set; } 

    #endregion Navigation Properties 
} 

उत्तर

8

विदेश प्रमुख विशेषता के लिए प्रयोग किया जाता है:

  1. नेविगेशन दी विदेशी कुंजी संपत्ति

    // this is foreign key property with related "privilegeLookup" navigation property. Database column name will be PrivilegeLookupId 
    [ForeignKey("privilegeLookup"), Column(Order = 1)]  
    public int PrivilegeLookupId { get; set; } 
    // this is related navigation property 
    public PrivilegeLookup privilegeLookup { get; set; } 
    
  2. से संबंधित संपत्ति के नाम का संकेत या के लिए विदेशी कुंजी संपत्ति के नाम का संकेत दी गई नेविगेशन संपत्ति:

    // this is foreign key property 
    public int PrivilegeLookupId { get; set; } 
    // this is navigation property with related foreign key property 
    [ForeignKey("PrivilegeLookupId")] 
    public PrivilegeLookup privilegeLookup { get; set; } 
    

यह उपयोगी है जब डिफ़ॉल्ट ईएफ कोड-प्रथम सम्मेलन लागू नहीं होते हैं, या आपके लिए उपयुक्त नहीं है। Here आप ईएफ कोड-प्रथम सम्मेलनों की एक सूची देख सकते हैं।

उलटा संपत्ति विशेषता का इस्तेमाल किया जब तुम वर्ग में है कि नेविगेशन संपत्ति इंगित करने के लिए एक वर्ग बी में एक और नेविगेशन संपत्ति के रूप में ही विदेशी कुंजी से संबंधित है की जरूरत है। उदाहरण के लिए:

public class Student 
{ 
    public int StudentID { get; set; } 

    public Standard CurrentStandard { get; set; } 
    public Standard PreviousStandard { get; set; } 
} 

public class Standard 
{  
    public int StandardId { get; set; } 

    public ICollection<Student> CurrentStudents { get; set; } 
    public ICollection<Student> PreviousStudents { get; set; } 
} 

यहाँ हम दो वर्गों, दो नेविगेशन गुणों के साथ प्रत्येक की है। हमारा इरादा टेबल छात्र में दो विदेशी कुंजी रखना है, संभवत: वर्तमान मानक मानक और पिछला मानक ID नाम दिया गया है, और कक्षा मानक के नेविगेशन गुण भी एक ही विदेशी कुंजी (कई रिश्ते में से एक) से संबंधित हैं। हालांकि, इस मामले में ईएफ को बिना किसी मार्गदर्शन के इसका एहसास होगा - इसके बजाय यह विदेशी कुंजी बनाएगा।

public class Standard 
{ 
    public int StandardId { get; set; } 

    // reference to the name of another navigation property in class Student 
    [InverseProperty("CurrentStandard")] 
    public ICollection<Student> CurrentStudents { get; set; } 

    // reference to the name of another navigation property in class Student 
    [InverseProperty("PreviousStandard")] 
    public ICollection<Student> PreviousStudents { get; set; } 
} 

अब एफई हमारे इरादों को समझता है और सिर्फ दो विदेशी कुंजी पैदा करेगा, हालांकि नाम अच्छा नहीं होगा: यह मार्गदर्शन के लिए, हम उलटा संपत्ति विशेषता का उपयोग करने के लिए है। स्तंभ नाम भी बदलने के लिए, हम विदेशी कुंजी विशेषता का उपयोग कर सकते हैं:

public class Student 
{ 
    public int StudentID { get; set; } 

    public int CurrentStandardId { get; set; } 
    public int PreviousStandardId { get; set; } 

    [ForeignKey("CurrentStandardId")] 
    public Standard CurrentStandard { get; set; } 

    [ForeignKey("PreviousStandardId")] 
    public Standard PreviousStandard { get; set; } 
} 

लंबी कहानी छोटी - एफई कई कोड सम्मेलनों के आधार पर चीजों को अनुमान लगा सकते हैं। हालांकि जब यह नहीं हो सकता है (उदाहरण के लिए जब आपके पास एक ही कक्षा में दो विदेशी कुंजी हैं) - आपको अपने प्रश्न से विशेषताओं का उपयोग करके इसकी सहायता करनी होगी।

+0

धन्यवाद @Evk संदेह को समाशोधन के लिए। – IAmDineshL

+0

इसे पूरा करने के लिए, संपत्ति 'सार्वजनिक आईसीओलेक्शन वर्तमान स्टुडेंट्स' को '[विदेशीकी (" CurrentStandardId ")] के साथ भी एनोटेट किया जा सकता है। यह इसे 'इनवर्क्सप्रोपर्टी' के करीब भ्रामक रूप से ले जाता है, लेकिन यह केवल एक प्राथमिक संपत्ति का संदर्भ ले सकता है, संदर्भ संदर्भ नहीं, जैसे 'वर्तमान मानक'। –

संबंधित मुद्दे