2014-07-25 3 views
0

:먼저 Entity Framework 코드에서 여러 엔터티를 정의하기 위해 SQL보기를 매핑 하시겠습니까? 나는 다음과 같은 기관에 SQL 테이블을 매핑하는 제 1 유동성 API에 접근 코드를 사용하고

public class AddressEntityMapper : EntityTypeConfiguration<AddressEntity> 
    { 
     public AddressEntityMapper() 
     { 
      ToTable("Address"); 
      HasKey(m => m.AddressID); 
      HasRequired(m => m.UserEntity).WithMany(b => b.AddressEntity); 
      Property(p => p.AddressKey).HasColumnName("ADDRESSID"); 
      Property(p => p.UserID).HasColumnName("User_id"); 

      Property(p => p.Address1).HasColumnName("ADDRESS1"); 
      Property(p => p.Address2).HasColumnName("ADDRESS2"); 
      Property(p => p.Address3).HasColumnName("ADDRESS3"); 
      Property(p => p.City).HasColumnName("CITY"); 
      Property(p => p.State).HasColumnName("STATE"); 
      Property(p => p.ZipCode).HasColumnName("ZIPCODE"); 
} 
} 

그리고 사용자 매퍼 :

public class UserEntityMapper : EntityTypeConfiguration<UserEntity> 
    { 
     public UserEntityMapper() 
     { 
      ToTable("User"); 

      HasKey(m => m.UserID); 
      Property(p => p.UserID).HasColumnName("UserID"); 
      Property(p => p.FirstName).HasColumnName("FIRSTNAME"); 
      Property(p => p.LastName).HasColumnName("LAST_NAME"); 
      Property(p => p.MiddleInitial).HasColumnName("MIDDLEINITIAL"); 
} 
} 

public class ProjectEntities : DbContext 
    { 
     public ProjectEntities() 
      : base("name=ProjectEntities ") 
     { 

     } 

     public DbSet<UserEntity> UserEntity { get; set; } 
     public DbSet<AddressEntity> AddressEntity { get; set; } 


     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 

      modelBuilder.Configurations.Add(new UserEntityMapper()); 
      modelBuilder.Configurations.Add(new AddressEntityMapper()); 
    } 
} 

그리고 매퍼로 delacred된다

이 물건은 잘 작동합니다. 이제 정보를 얻기 위해 로직을 변경해야합니다. 이제 회사는 완전한 사용자 정보를 얻기 위해 sql보기를 사용하기로 결정 했으므로 사용자의 모든 정보를 한 곳에서 얻을 수 있습니다. 이제는 재실행/모든 필드를 매핑하는 다른 매퍼를 만들지 않아도됩니다. 두 테이블 모두 SQL 뷰에서 가져옵니다.

public class UserFullEntityMapper : EntityTypeConfiguration<UserEntity, AddressEntity> 
    { 
     public UserFullEntityMapper() 
     { 
      ToTable("vw_user"); 

      ///Declare the auto mapping here to the fields from above entities to columns retuned from sql view. 
} 
} 

제안하십시오 :

그래서 제 질문은 우리가 이러한 매퍼의 두 매핑 다른 매퍼를 선언 할 수있다. 감사

당신은 당신의보기의 새로운 엔티티를 정의해야

답변

0

:

public View_mapping:EntityTypeConfiguration<YourViewName> 
{ 
    public View_mapping() 
    { 
     ToTable("vw_user"); 
     // other mappings 
    } 
} 
: 매핑 다음

public class YourViewName 
{ 
    // properties from UserEntity 
    // properties from AddressEntity 
} 

및 사용