2017-02-24 8 views
5

मैं निम्नलिखित सार वर्ग, Sector नामित किया है:इकाई फ्रेमवर्क कोर: व्युत्पन्न रूप से डीबीसेट को व्युत्पन्न प्रकार से कैसे प्राप्त करें?

public abstract class Sector 
{ 
    public string ID {get; set;} 
    public string Name {get; set;} 
    public Sector(){} 
} 

और एक द्वितीय श्रेणी, GICSSector, जो Sector से विरासत:

public class GICSSector: Sector 
{ 
    public virtual ICollection<GICSIndustryGroup> IndustryGroups {get; set;} 
} 

मैं अपने DbContext में DbSet निम्नलिखित है:

public DbSet<GICSSector> GICSSectors {get; set;} 

मैं लोड करने के लिए एक सामान्य विधि लिखने की कोशिश कर रहा हूं अता CSV फ़ाइल से, मक्खी पर वस्तुओं को बनाने और उसके बाद मेरे SQLLite डेटाबेस में वस्तुओं की दुकान:

public static void UpdateEntitiesFromCSV<T>(MyContextFactory factory, string fileName) where T : class 
{ 
    var entities = new List<T>(); 

    // ... Load entities from the CSV 
    // ... Create the objects and add them to the list 

    // Add the objects to the database 

    using (var db = factory.Create(new DbContextFactoryOptions())) 
    { 
     var set = db.Set<T>(); 

     foreach(T e in entities) 
     { 
      set.Add(e); 
     } 

     db.SaveChanges(); 
    } 

} 

मैं टेबल प्रबंधन करने के लिए धाराप्रवाह एपीआई का उपयोग:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    //... 

    // GICSSector  
    modelBuilder.Entity<GICSSector>().HasKey(s => new { s.ID }); 
    modelBuilder.Entity<GICSSector>().Property(s => s.ID).HasMaxLength(2);  
    modelBuilder.Entity<GICSSector>().Property(s => s.Name).IsRequired();  
    modelBuilder.Entity<GICSSector>().Property(s => s.Name).HasMaxLength(50); 
} 

अगर मैं चलाने कोड मुझे निम्नलिखित अपवाद मिलता है: SQLite त्रुटि 1: 'ऐसी कोई तालिका नहीं: सेक्टर'

अगर मैं typeof(T) या का उपयोग कर myEntity.GetType() साथ प्रकार की जांच मैं एक ही उम्मीद परिणाम प्राप्त: MyNamespace.GICSSector

क्यों एफई कोर एक मेज "क्षेत्र" (आधार प्रकार) और में नहीं की उम्मीद बुलाया में संग्रहीत करना चाहता है GICSSectors?

मैं इसे कैसे ठीक कर सकता हूं?

नोट: विधि एक सामान्य है जिसका उपयोग केवल Sector से प्राप्त होने वाले वर्गों को संभालने के लिए नहीं किया जाएगा।

+0

TableAttribute साथ GICSSector वर्ग पर टिप्पणी करें? – MarkB

+0

मैं धाराप्रवाह एपीआई का उपयोग करता हूं, मैंने –

+0

प्रश्न अपडेट किया है फिर तालिका नाम सेट करने के लिए धाराप्रवाह API का उपयोग करें? (मेरा जवाब देखें) – MarkB

उत्तर

0

एफई बताओ स्पष्ट रूप से क्या तालिका का उपयोग करने के लिए:

[Table("GICSSectors")] 
public class GICSSector: Sector 
{ 
    public virtual ICollection<GICSIndustryGroup> IndustryGroups {get; set;} 
} 

या धाराप्रवाह एपीआई का उपयोग करें:

modelBuilder.Entity<GICSSector>().ToTable("GICSSectors"); 
संबंधित मुद्दे