2012-11-18 12 views
5

基本オブジェクトのプロパティの中心的なマッピングを取得するにはいくつかのトリックがありますか? EntityTypeConfigurationを使用しているときに、抽象クラスの単純なパターンがありますか。
何かヒントありがとうございます。クラスに私は答えが、私はそれを割れて6時間後 How to create and use a generic class EntityTypeConfiguration<TEntity>Dynamic way to Generate EntityTypeConfiguration : The type 'TResult' must be a non-nullable value typeEntityTypeConfigurationを使用する場合の抽象ドメインモデルの基本クラス<T>

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

を仕事を得るcouldntの

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T> 

同様の問題を宣言することができませんでし イム。私はそれが合理的にきれいな結果だと思う。 このトリックは、EntityTypeConfiguration から派生したクラスをすべて忘れてカスタムBaseConfigを構築し、このインスタンスを取得してこのクラスの仕様を追加することです。これはわずかにきれいにし、DbContextで設定を登録するときに、同じ作業の利点を持っているかもしれませんが、間違いなく動作します上記の回答

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

...それは他の人が抄録で最初のコードを実行するのに役立ちます願っています。溶液上の

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()); 
    } 
} 

はクリスチャン・ウィリアムズによりに到着し、私自身早く1つの朝...

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 
の周りに次の2行を入れ替えます

これは、特殊フィールドを持つEFに役立ちます。

関連する問題