2017-01-19 1 views
0

처음에는 Expression 및 Func과 함께 초보자입니다.일반 엔티티 프레임 워크 매핑

내 EF 매핑 클래스에서 코드 중복을 피하려고하는데 잘못된 데이터베이스가 붙어 있습니다.

public class EntityMap : EntityTypeConfiguration<Entity> 
{ 
    public EntityMap() 
    { 
     Property(x => x.PropertyA.Property); 
     Property(x => x.PropertyB.Property); 
    } 
} 

PropertyAPropertyB가 동일한 유형, 많은 재산 으로는, 간단한 방법으로이 리팩토링 매개 변수에 x => x.PropertyA 또는 PropertyB을 통과 할 수 있나요 :

다음 예제 맵 클래스를 가지고 Property(x => x. methodParemeter Property);과 같은 무엇입니까? 그리고 어떻게 ? 메소드는 다음과 같을 수 있습니다.

private void SubMap(Expression<Func<Entity, SubEntity>> propertyExpression, string prefix) 
{ 
    Property(x => x.propertyExpression.Property) 
      .HasColumnName(string.Format("{0}{1}", prefix,"columnName")); 
} 
+0

어떻게 개선 될 수 있습니까? – DavidG

+0

질문을 이해하지 못한다. 클래스 A 속성을 클래스 B에 자동으로 매핑 하시겠습니까? –

+0

대신 .Property는 예를 들어'.MaxLength (50)'을 의미하며, 여러 속성에 대해 더 밀도가 높은 구문으로 호출하려고합니다 ? 그래서 당신은'{f => f.PropertyA, f => f.PropertyB} .ForEach (p => p.MaxLength (50))'와 같은 것을 원합니까? – CodeCaster

답변

0

기본 클래스와 인터페이스로 수행 할 수 있습니다.

모델

public interface IEntity 
{ 
    string PropertyA { get; set; } 
    string PropertyB { get; set; } 
} 

public class EntityA : IEntity { 
    public string PropertyA { get; set; } 
    public string PropertyB { get; set; } 
} 

public class EntityB : IEntity 
{ 
    public string PropertyA { get; set; } 
    public string PropertyB { get; set; } 
} 

기본 클래스

public abstract class IEntityMap<T> : EntityTypeConfiguration<T> where T : class, IEntity 
{ 
    protected IEntityMap() 
    { 
     this.Property(x => x.PropertyA); 
     this.Property(x => x.PropertyB); 
    } 
} 

매퍼 구현

당신의 DbContext 유형이 등록합니다.

public class EntityAMap : IEntityMap<EntityA> 
{ 
    public EntityAMap() : base() 
    { 
    } 
} 

public class EntityBMap : IEntityMap<EntityB> 
{ 
    public EntityBMap() : base() 
    { 
    } 
} 
관련 문제