2012-11-18 12 views
5

का उपयोग करते समय सार डोमेन मॉडल बेस क्लास बेस ऑब्जेक्ट गुणों का केंद्रीय मैपिंग प्राप्त करने के लिए कुछ चाल है? EntityTypeConfiguration का उपयोग करते समय अमूर्त कक्षाओं के लिए कुछ सरल पैटर्न है।
किसी भी सुझाव की बहुत सराहना की। इम एक वर्गEntityTypeConfiguration <T>

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T> 

इसी तरह के मुद्दों घोषित करने के लिए, मैं जहां सके जवाब काम करने के लिए How to create and use a generic class EntityTypeConfiguration<TEntity> और Dynamic way to Generate EntityTypeConfiguration : The type 'TResult' must be a non-nullable value type

public abstract class BosBaseObject 
{ 
    public virtual Guid Id { set; get; } 
    public virtual string ExternalKey { set; get; } 
    public byte[] RowVersion { get; set; } 
} 
    public class News : BosBaseObject 
{ 
    public String Heading { set; get; } 
} 


public class NewsMap : EntityTypeConfiguration<News> 
{ 
    public NewsMap() 
    { 
     //Base Object Common Mappings 
     // How can we use a central mapping for all Base Abstract properties 


    } 
} 
// Something like this but very open to any suggestion.... 
public class BosBaseEntityConfig<T> : EntityTypeConfiguration<T> 
{ 
    public void BaseObjectMap() 
    { 
     // Primary Key 
     this.HasKey(t => t.Id); 

     // Properties 
     this.Property(t => t.Id).HasDatabaseGeneratedOption(databaseGeneratedOption: DatabaseGeneratedOption.None); 

     this.Property(t => t.RowVersion) 
      .IsRequired() 
      .IsFixedLength() 
      .HasMaxLength(8) 
      .IsRowVersion(); 

     //Column Mappings 
     this.Property(t => t.Id).HasColumnName("Id"); 
    } 
} 

उत्तर

3

6 बजे मैं यह फटा बाद मिल में असमर्थ है। मुझे लगता है कि यह एक उचित साफ परिणाम है। यह चाल EntityTypeConfiguration से प्राप्त कक्षा के अंदर हर किसी को भूलना और कस्टम बेसकॉन्फ़िग बनाना और फिर इस उदाहरण को लेने और इस कक्षा के लिए विशिष्टताओं को जोड़ने के लिए है। आशा है कि यह दूसरों के सार के साथ पहली बार कोड कर में मदद करता है ...

public abstract class BosBaseObject 
{ 
    public virtual Guid Id { set; get; } 
    public virtual string ExternalKey { set; get; } 
    public byte[] RowVersion { get; set; } 
} 
public abstract class BosObjectDateManaged : BosBaseObject 
{ 
    public DateTimeOffset ValidFrom { set; get; } 
    public DateTimeOffset ValidTo { set; get; } 
} 
public class News : BosObjectDateManaged 
{ 
    public String Heading { set; get; } 
} 



protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     var conf = new BosBaseEntityConfiguration<News>();//Construct config for Type 
     modelBuilder.Configurations.Add(conf); // this has base mapping now 
     var newsConf = new NewsConfiguration(conf); // now the Object specific properties stuff 

    } 

} 
public class BosBaseEntityConfiguration<T> : EntityTypeConfiguration<T> where T : BosBaseObject 
{ 
    public BosBaseEntityConfiguration() 
    { 
     // Primary Key 
     this.HasKey(t => t.Id); 

     //// Properties 
     this.Property(t => t.Id).HasDatabaseGeneratedOption(databaseGeneratedOption: DatabaseGeneratedOption.None); 

     this.Property(t => t.RowVersion) 
      .IsRequired() 
      .IsFixedLength() 
      .HasMaxLength(8) 
      .IsRowVersion(); 

     //Column Mappings 
     this.Property(t => t.Id).HasColumnName("Id"); 
    } 
} 
public class NewsConfiguration 
{ 
    public NewsConfiguration(BosBaseEntityConfiguration<News> entity) 
    { 
     // Table Specific & Column Mappings 
     entity.ToTable("News2"); 
     entity.Property(t => t.Heading).HasColumnName("Heading2"); 
    } 
} 
6

ऊपर जवाब निश्चित रूप से, काम करता है, हालांकि यह थोड़ा क्लीनर हो सकता है और एक ही काम कर रहा है जब DbContext में विन्यास दर्ज की का लाभ दिया है हो सकता है।

public abstract class BaseEntity 
{ 
    public int Id { get; set; } 
} 

public class Company : BaseEntity 
{ 
    public string Name { get; set; } 
} 

internal class BaseEntityMap<T> : EntityTypeConfiguration<T> where T : BaseEntity 
{ 
    public BaseEntityMap() 
    { 
     // Primary Key 
     HasKey(t => t.Id); 
    } 
} 

internal class CompanyMap : BaseEntityMap<Company> 
{ 
    public CompanyMap() 
    { 
     // Properties 
     Property(t => t.Name) 
      .IsRequired() 
      .HasMaxLength(256); 
    } 
} 

public class AcmeContext : DbContext 
{ 
    public DbSet<Company> Companies { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Configurations.Add(new CompanyMap()); 
    } 
} 

समाधान ऊपर ईसाई विलियम्स द्वारा पर पहुंचे और अपने आप को जल्दी एक सुबह ...

0

खेद है कि मैं टिप्पणी नहीं कर सकता, लेकिन मैं के रूप में आप केवल कर रहे हैं

 modelBuilder.Configurations.Add(conf); // this has base mapping now 
     var newsConf = new NewsConfiguration(conf); // now the Object specific properties stuff 
    to 
     new NewsConfiguration(conf); // now the Object 
     modelBuilder.Configurations.Add(conf); // this has base mapping now 
चारों ओर इन दो पंक्तियों स्वैप करना होगा

यह विशेष क्षेत्रों के साथ ईएफ में मदद करता है।

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