2011-03-30 23 views
24

के लिए कुंजी परिभाषित करें हाय मैं अपनी परियोजना में से किसी एक में ईएफ कोड का परीक्षण करने की योजना बना रहा हूं। यह वही है जो मैं वास्तव में चाहता हूं। मैं तीन टेबल है और संरचना के रूप मेंइकाई फ्रेमवर्क कोड-फर्स्ट - इस एंटीटी टाइप

public partial class App_user 
    { 
     public int id { get; set; } 
     public string name { get; set; } 
     public string email_address { get; set; } 
     public string password { get; set; } 
     public int user_type { get; set; } 
     public List<Role> Roles { get; set; } 
    } 

public partial class Role 
    { 
     public int id { get; set; } 
     public string name { get; set; } 
    } 
public partial class User_role 
    { 
     public int user_id { get; set; } 
     public int role_id { get; set; } 
     public virtual Role Role { get; set; } 
     public virtual App_user App_user { get; set; } 
    } 

तीसरे तालिका में कोई प्राथमिक कुंजी है इस प्रकार है। तो यह चलते समय एक त्रुटि देता है। : EntityType 'USER_ROLE' कोई कुंजी परिभाषित किया गया है:

System.Data.Edm.EdmEntityType - यहाँ त्रुटि संदेश है। इस EntityType के लिए कुंजी को परिभाषित करें। System.Data.Edm.EdmEntitySet: EntityType: EntitySet User_roles टाइप User_role पर आधारित है जिसमें कुंजी परिभाषित नहीं है।

ऐसा क्यों हो रहा है? क्या इसका कोई समाधान है?

उत्तर

37

तो लगता है कि आप कई-से-अनेक उपयोगकर्ता और भूमिका के बीच संबंध मॉडल करने के लिए कोशिश कर रहे हैं। ऐसे मामले में आपका मॉडल पूरी तरह गलत है।

उपयोग इस बजाय:

public partial class App_user 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string email_address { get; set; } 
    public string password { get; set; } 
    public int user_type { get; set; } 
    public virtual ICollection<Role> Roles { get; set; } 
} 

public partial class Role 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public virtual ICollection<User> Users { get; set; } 
} 

यह स्वचालित रूप से कई-से-अनेक पैदा करेगा और आप जंक्शन तालिका के साथ परेशान करने के लिए जरूरत नहीं होगी। आप इस का उपयोग करना चाहिए आप जंक्शन तालिका का पर्दाफाश करने की जरूरत है: यदि आप इसे में अतिरिक्त गुणों की जरूरत नहीं है

public partial class App_user 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string email_address { get; set; } 
    public string password { get; set; } 
    public int user_type { get; set; } 
    public virtual ICollection<User_Role> UserRoles { get; set; } 
} 

public partial class Role 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public virtual ICollection<User_Role> UserRoles { get; set; } 
} 

public partial class User_role 
{ 
    [Key, ForeignKey("App_user"), Column(Order = 0)] 
    public int user_id { get; set; } 
    [Key, ForeignKey("Role"), Column(Order = 1)] 
    public int role_id { get; set; } 
    public virtual Role Role { get; set; } 
    public virtual App_user App_user { get; set; } 
} 

उजागर जंक्शन तालिका अर्थहीन है।

आपकी त्रुटि के लिए - इकाई फ्रेमवर्क में प्रत्येक इकाई में प्राथमिक कुंजी परिभाषित होनी चाहिए।

+1

[कुंजी, विदेशीकी ("App_user"), कॉलम (ऑर्डर = 0) ] - इसके साथ उपयोग करने के लिए नामस्थान क्या है? – Mukesh

+4

सिस्टम। कॉम्पोनेंट मॉडेल। डेटाएनोटेशन (इसमें अलग असेंबली भी है)। –

+0

@LadislavMrnka, मैं लागू करने के लिए '[कुंजी, ForeignKey (" App_user "), स्तंभ (आदेश = 0)] नहीं है', केवल [Key] उपलब्ध है, क्या मुझे याद आ रही? धन्यवाद। – Abhijeet

15

आप बस इस तरह कर सकते हैं:

public partial class User_role 
{ 
    [Key] 
    public int user_id { get; set; } 
    [Key] 
    public int role_id { get; set; } 
    public virtual Role Role { get; set; } 
    public virtual App_user App_user { get; set; } 
} 
+0

कि user_id कुंजी नहीं होगा? क्या होगा अगर उपयोगकर्ता कई भूमिकाओं में है? – archil

+0

इस तरह कुछ के बारे में क्या? –

+0

@ YngveB.Nilsen आप [कुंजी] संपत्ति कैसे जोड़ते हैं? मैंने System.ComponentModel.DataAnnotation की पेशकश की। लेकिन यह केवल एक अलग निर्देशिका के तहत [विदेशी] [कुंजी] दिखाता है? – CodeEngine

1

मैं स्वचालित उत्पन्न कोड के साथ एक ADO.net Entiry डेटा मॉडल का इस्तेमाल किया क्योंकि मैं मॉडल वर्गों को बदलने के लिए नहीं करना चाहता था इसलिए मैं WebApiConfig.cs अंतर्निहित मॉडल की कुंजी परिभाषित करने के लिए बदल दिया है:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); 
EntitySetConfiguration<Registration> EntitySetConfiguration_Registration = builder.EntitySet<Registration>("Registration"); 
EntitySetConfiguration<Lead> EntitySetConfiguration_Lead = builder.EntitySet<Lead>("Lead"); 
EntitySetConfiguration<Session> EntitySetConfiguration_Session = builder.EntitySet<Session>("Session"); 
EntitySetConfiguration_Registration.EntityType.HasKey(p => p.Registration_Id); 
EntitySetConfiguration_Lead.EntityType.HasKey(p => p.Lead_Id); 
EntitySetConfiguration_Session.EntityType.HasKey(p => p.Session_Id); 
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel()); 
0

सुनिश्चित करें कि आप प्रयोग कर रहे हैं (System.ComponentModel.DataAnnotations उपयोग करते हुए;)

बस मॉडल में डेटा क्षेत्र के शीर्ष पर कीवर्ड [Key] जोड़ने

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