2012-11-18 33 views
5

Có một số mẹo để lập bản đồ trung tâm của các thuộc tính đối tượng cơ sở không? Có một số mẫu đơn giản cho các lớp trừu tượng khi sử dụng EntityTypeConfiguration hay không.
BẤT K tips mẹo được đánh giá cao. Im không thể khai báo một lớpLớp cơ sở mô hình miền trừu tượng khi sử dụng EntityTypeConfiguration <T>

Public class BaseEntityConfig<T> : EntityTypeConfiguration<T> 

vấn đề tương tự, trong đó i couldnt nhận được câu trả lời cho việc 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"); 
    } 
} 

Trả lời

3

Sau 6 giờ tôi nứt nó. Tôi nghĩ rằng đó là một kết quả hợp lý sạch sẽ. Bí quyết là quên làm mọi thứ bên trong một lớp có nguồn gốc từ EntityTypeConfiguration và xây dựng một BaseConfig tùy chỉnh và sau đó lấy ví dụ này và thêm các chi tiết cụ thể cho lớp này. Hy vọng nó sẽ giúp những người khác làm mã đầu tiên với tóm tắt ...

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

Câu trả lời ở trên chắc chắn công trình, mặc dù điều này có thể nhẹ sạch hơn và có lợi thế là làm việc cùng khi đăng ký các cấu hình trong 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()); 
    } 
} 

Trên giải pháp đến Christian Williams và bản thân mình đầu một buổi sáng ...

0

Xin lỗi tôi không thể bình luận nhưng tôi sẽ làm như bạn đang làm chỉ trao đổi hai dòng sau khoảng

 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 

Điều này giúp EF với các lĩnh vực chuyên môn.

Các vấn đề liên quan