2012-02-08 2 views
0

중지 반복에 거의 반영되지 않은 유창한 API를 사용하려면 어떻게해야합니까?반영 유형의 EF Fluent API는 어떻게 사용할 수 있습니까?

샘플 코드;

public abstract class Entity : IEntity 
{ 
    public int Id { get; set; } 
} 

public class SemiStructuredEntity : Entity 
{ 
    public virtual int DocumentId { get; set; } 

    [ForeignKey("DocumentId")] 
    public virtual Document Document { get; set; } 
} 

public class Payment : SemiStructuredEntity 
{ 
    public string Code { get; set; } 
} 

public class Member : SemiStructuredEntity 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 

public class Document : Entity 
{ 
    public string UniversalId { get; set; } 
    public string Subject { get; set; } 
} 

및 유창;

modelBuilder.Entity<Member>() 
     .HasRequired(a => a.Document) 
     .WithMany() 
     .HasForeignKey(u => u.DocumentId); 

    modelBuilder.Entity<Payment>() 
      .HasRequired(a => a.Document) 
      .WithMany() 
      .HasForeignKey(u => u.DocumentId); 

우리는 우리가 또 다른 유창을 정의해야합니다 SemiStructuredEntity 기본 클래스에서 상속 다른 클래스가 필요합니다.

내 꿈은, 사전에

List<Type> targetTypes = getSemiStructuredEntities(); 
    /// 
    ///and now How? How can i define fluent for all of targetTypes in second statements? 
    /// 

또는

List<Type> targetTypes = getSemiStructuredEntities(); 
foreach (Type item in targetTypes) 
{ 
      /// 
      ///And now How? How can i define fluent for this type 
      /// 
} 

감사 (내 질문은 코드 블록의 행을 주석).

답변

1

왜 리플렉션을 사용합니까? EntityTypeConfiguration<>에서 파생 된 유형을 사용하고 OnModelCreation 메소드에서 modelBuilder.Configurations에 이러한 유형을 등록하여 매핑 in reusable way을 정의 할 수 있습니다.

관련 문제