2011-06-08 6 views
9

मैं ईएफ कोड फर्स्ट का उपयोग करके दो इकाइयों के बीच द्वि-दिशात्मक वन-वन संबंध बनाना चाहता हूं। मुझे निम्नलिखित कोड में परेशानी है। तुम्हे क्या लगता है मैं क्या करूँ?बायिडायरेक्शनल वन बनाना - इकाई फ्रेमवर्क में एक रिश्ते 4.1 कोड प्रथम

public class User 
{ 
    public string ID { get; set; } 
    public string LastName { get; set; } 
    public string Password { get; set; } 
    public string FirstName { get; set; } 

    public int ProfileID { get; set; } 
    public Profile Profile { get; set; } 

} 
public class Profile 
{ 
    public int UserID { get; set; } 
    public User User { get; set; } 
    public int ProfileID { get; set; } 
    public string ProfileName { get; set; } 

    public DateTime CreateDate { get; set; } 
    public DateTime LastUpdateDate { get; set; } 

} 

मैं दोनों इकाइयों में नेविगेशन संपत्ति और विदेशी कुंजी दोनों रखना चाहता हूं।

यह मुझे त्रुटि देता है। इस काम को करने के लिए फ्लुएंट मैपिंग एपीआई में मैं क्या कर सकता हूं?

+0

आपको क्या त्रुटि है? – Eranga

उत्तर

17

इस का उपयोग करें:

public class User 
{ 
    public string ID { get; set; } 
    public string LastName { get; set; } 
    public string Password { get; set; } 
    public string FirstName { get; set; } 
    public Profile Profile { get; set; } 
} 

public class Profile 
{ 
    [Key, ForeignKey("User")] 
    public int ProfileID { get; set; } 
    public string ProfileName { get; set; } 
    public DateTime CreateDate { get; set; } 
    public DateTime LastUpdateDate { get; set; } 
    public User User { get; set; } 
} 

एफई में एक-से-एक संबंध का निर्माण करने की वैध तरीका है कि - निर्भर इकाई के पी भी प्रमुख संस्था के लिए FK होना चाहिए। ईएफ में बिडरेक्शनल एक-से-एक संबंध की तरह कुछ भी नहीं है क्योंकि यह ईएफ में काम नहीं कर सकता है।

जिस तरह से लोग कभी-कभी इसे दूर करते हैं, वे दो-से-कई संबंध होते हैं जहां प्रिंसिपल में निर्भर संस्थाओं के लिए नेविगेशन संग्रह नहीं होता है + डेटाबेस में मैन्युअल रूप से परिभाषित अद्वितीय कुंजी। यही कारण है कि मैनुअल मानचित्रण की आवश्यकता होती है:

public class User 
{ 
    public string ID { get; set; } 
    public string LastName { get; set; } 
    public string Password { get; set; } 
    public string FirstName { get; set; } 
    // one side MUST be nullable otherwise you have bidirectional constraint where each 
    // entity demands other side to be inserted first = not possible 
    public int? ProfileId { get; set; } 
    public Profile Profile { get; set; } 
} 

public class Profile 
{ 
    public int ProfileID { get; set; } 
    public string ProfileName { get; set; } 
    public DateTime CreateDate { get; set; } 
    public DateTime LastUpdateDate { get; set; } 
    public int UserId { get; set; } 
    public User User { get; set; } 
} 

और मानचित्रण में आप परिभाषित करेगा:

modelBuilder.Entity<User> 
      .HasOptional(u => u.Profile) 
      .WithMany() 
      .HasForeignKey(u => u.ProfileId); 
modelBuilder.Entity<Profile> 
      .HasRequired(u => u.User) 
      .WithMany() 
      .HasForeignKey(u => u.UserId); 

अब आप डेटाबेस में अद्वितीय कुंजी को परिभाषित करना होगा - आप कोड का उपयोग कर रहे हैं पहले custom database initialize आर का उपयोग करें। ध्यान रखें कि अभी भी द्विपक्षीय एक-से-एक गलत अवधारणा है क्योंकि दोनों पक्ष अद्वितीय एफके की मांग करते हैं जहां NULL अभी भी अद्वितीय मानों में शामिल है, इसलिए UserProfile से पहले UserProfile के बिना कोई अन्य 0 नहीं होना चाहिए। यह शायद serializable लेनदेन की ओर जाता है।

+0

Ladislav, अब यह सही समझ में आता है ... मैं समझता हूं कि रिश्तों को कैसे बनाया जाता है ... हालांकि मैं मानता हूं कि इसे बेहतर तरीके से समर्थित किया जाना चाहिए जिससे अद्वितीय कुंजी मैन्युअल रूप से बनाई जा सके ... – InvisibleDev

+0

क्यों कई यदि यह एक- एक संबंध? – angel

+0

@angel: यह ईएफ की सीमाओं को दूर करने के लिए एक कामकाज है - उत्तर का दूसरा भाग यह कहकर ईएफ को धोखा दे रहा है कि एक-से-एक सीधे मैपिंग करने के बजाय दो-से-कई संबंध हैं। विदेशी कुंजी कॉलम पर अद्वितीय कुंजी बाधा डालकर डेटाबेस स्तर पर एक-से-एक सुनिश्चित किया जाता है। –

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