2013-10-13 12 views
36

यहाँ मामला ऐसा लगता है कि वे कई लोगों के लिए कई हैं कि हैएंटिटी फ्रेमवर्क में कई से कई मैपिंग कैसे बनाएं?

public class Media : Entity 
{ 
    public string Name {get; set;} 
    public bool Enabled 
    *//other properties can be ignored..* 
} 

public class Contract : Entity 
{ 
    public string Code {get; set;} 
    *//other properties can be ignored..* 
} 

अनुबंध कई Medias है मैं इस तरह के अनुबंध के रूप में 2 संस्थाओं,, है मीडिया।,,।

लेकिन !! पहले एफई कोड पर, मुझे कॉन्ट्रैक्टमीडिया टेबल (ef auto जेनरेट) में 3 और फ़ील्ड चाहिए। जैसे स्टार्टडेट, एंडडेट और मूल्य। इन्हें मीडिया इकाई में जोड़ा नहीं जा सका।

इस मामले में कैसे मानचित्र करें ??

+0

की संभावित डुप्लिकेट [पहले कोड बनाएँ, कई के लिए कई, संघ तालिका में अतिरिक्त क्षेत्रों के साथ] (http://stackoverflow.com/questions/7050404/create-code-first-many-to-many- अतिरिक्त-फ़ील्ड-इन-एसोसिएशन-टेबल) – forsvarir

उत्तर

73

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

आप में मामला यह हो जाएगा:

public class Media // One entity table 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public bool Enabled { get; set; } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class Contract // Second entity table 
{ 
    public int Id { get; set; } 
    public string Code { get; set } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class ContractMedia // Association table implemented as entity 
{ 
    public int MediaId { get; set; } 
    public int ContractId { get; set; } 
    public DateTime StartDate { get; set; } 
    public DateTime EndDate { get; set; } 
    public double Price { get; set; } 

    public virtual Media Media { get; set; } 
    public virtual Contract Contract { get; set; } 
} 

और तुम्हारे जाने के बाद मॉडल/संस्थाओं बनाए हैं तो आप संदर्भ में संबंधों को परिभाषित करने की जरूरत है:

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<ContractMedia>() 
     .HasKey(c => new { c.MediaId, c.ContractId }); 

    modelBuilder.Entity<Contract>() 
     .HasMany(c => c.ContractMedias) 
     .WithRequired() 
     .HasForeignKey(c => c.ContractId); 

    modelBuilder.Entity<Media>() 
     .HasMany(c => c.ContractMedias) 
     .WithRequired() 
     .HasForeignKey(c => c.MediaId); 
} 

इसके अलावा, आप इन कड़ियों का उल्लेख कर सकते:
Many to many mapping with extra fields in Fluent API
Entity Framework CodeFirst many to many relationship with additional information
Create code first, many to many, with additional fields in association table

+5

मुझे लगता है कि 'अनुबंध मीडिया' में व्यस्त एनवी संग्रह नहीं होना चाहिए: 'मीडिया' और 'अनुबंध'। इन्हें इसके बजाय नौसेना गुणों को आगे बढ़ाया जाना चाहिए। मुझे कई-लुकअप टेबल में अतिरिक्त फ़ील्ड मिल रहे थे जब तक कि मैंने गुणों को आगे बढ़ाने के लिए उलटा (संग्रह) गुण नहीं बदला। – IAbstract

+0

@IAbstract मुझे लगता है कि आप सही हो सकते हैं क्योंकि मैं नेविगेशन से सामान्य तालिका में मूल्य पुनर्प्राप्त नहीं करता हूं। और मुझे यकीन है कि यह एनएवी संग्रह –

+0

@IAbstract के कारण है: आप गुणों को आगे बढ़ाने के लिए "उलटा (संग्रह) गुण कैसे बदलते हैं।" – eugenekgn

1

Fluent API का उपयोग किये बिना @ टोमास उत्तर में जोड़ना।

public class Media // One entity table 
{ 
    public int Id { get; set; } 

    public string Name { get; set; } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class Contract // Second entity table 
{ 
    public int Id { get; set; } 

    public string Code { get; set } 

    public virtual ICollection<ContractMedia> ContractMedias { get; set; } 
} 

public class ContractMedia // Association table implemented as entity 
{ 
    [Key] 
    [Column(Order = 0)] 
    [ForeignKey("Media")] 
    public int MediaId { get; set; } 

    [Key] 
    [Column(Order = 1)] 
    [ForeignKey("Contract")] 
    public int ContractId { get; set; } 

    public DateTime StartDate { get; set; } 

    public DateTime EndDate { get; set; } 

    public double Price { get; set; } 

    public virtual Media Media { get; set; } 

    public virtual Contract Contract { get; set; } 
} 
संबंधित मुद्दे