2012-11-18 14 views
5

C'è qualche trucco per ottenere una mappatura centrale delle proprietà dell'oggetto Base? Esiste un modello semplice per le classi astratte quando si utilizza EntityTypeConfiguration.
ANy suggerimenti molto apprezzati. Im in grado di dichiarare una classeClasse di base del modello astratto del dominio quando si utilizza EntityTypeConfiguration <T>

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T> 

problemi simili, in cui ho potuto ottenere le risposte a lavorare How to create and use a generic class EntityTypeConfiguration<TEntity> e 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"); 
    } 
} 

risposta

3

Dopo 6 ore ho rotto esso. Penso che sia un risultato abbastanza pulito. Il trucco consiste nel dimenticare di eseguire tutti all'interno di una classe derivata da EntityTypeConfiguration e creare un BaseConfig personalizzato e quindi prendere questa istanza e aggiungere le specifiche per questa classe. Speranza che aiuta gli altri a fare il codice prima con abstract ...

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

La risposta di cui sopra sicuramente funziona, anche se questo può essere lieve pulito e ha il vantaggio di lavorare lo stesso al momento della registrazione delle configurazioni del 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()); 
    } 
} 

Sopra soluzione arrivati ​​a da Christian Williams e io una mattina presto ...

0

Mi dispiace non posso commentare ma vorrei fare come si sta facendo solo scambiare queste due linee intorno

 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 

Questo aiuta EF con campi specializzati.

Problemi correlati