2012-11-18 10 views
5

Temel nesne özelliklerinin merkezi bir eşleştirmesini elde etmek için bir kandırmaca var mı? EntityTypeConfiguration kullanırken soyut sınıflar için bazı basit desen var mı.
HERHANGİ TAVSİYELER çok beğenildi. i cevaplar How to create and use a generic class EntityTypeConfiguration<TEntity> ve Dynamic way to Generate EntityTypeConfiguration : The type 'TResult' must be a non-nullable value typeEntityTypeConfiguration kullanırken soyut etki alanı modeli temel sınıfı <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"); 
    } 
} 

cevap

3

bunu kırık 6 saat sonra çalışmaya alamadım nerede, bir sınıf

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T> 

Benzer sorunları bildirmek için Im yapamaz. Bence bu oldukça temiz bir sonuç. Hile, EntityTypeConfiguration 'dan türetilen bir sınıf içinde her şeyi yapmayı unutmaya ve özel bir BaseConfig oluşturmaya ve bu örneği almak ve bu sınıf için ayrıntıları eklemek için unutulmamalıdır. ... başkalarının Soyutlu ilk kod yapıyor yardımcı

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

bu hafif daha temiz ve DBContext içinde yapılandırmaları kaydederken aynı çalışma avantajına sahiptir olsa yukarıdaki cevabı kesinlikle işe yarar. çözümü Üstü

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

Christian Williams geldi ve kendimi erken bir sabah ...

0

Üzgünüm yorum yapamam ama sadece yapıyorsun yapın

 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 
etrafında bu iki satırı takas olur

Bu, EF'ye uzmanlık alanları ile yardımcı olur.

İlgili konular