2012-04-01 2 views
0

나는 다음과 같은 엔티티가 :배수 개체 관계

  • 고객
  • 회사
  • 종업원
  • 주문
  • 주문-요소 그런

나는 다른이 Notes라는 엔티티. 각 엔티티는 많은 노트를 가질 수 있습니다. 코드를 처음으로 유창하게 API로 구현하는 방법은 무엇입니까? 목표는 Notes 테이블에 오브젝트 ID를 갖는 것이며이 엔티티 ID는 적절한 엔티티를 가리 킵니다.

답변

0

엔티티 프레임 워크는

내 법과를 (NHibernate에 같은 다른 프레임 워크 수행) 상자 밖으로 이기종 연결을 지원하지 않습니다 임시 엔티티에 대한 메모를 저장하고 검색하는 NoteService을 사용하는 것이 좋습니다.

public ICollection<Note> GetNotes(IEntity entity) 
{ 
    var entityType = ObjectContext.GetObjectType(entity.GetType()).Name; 
    return context.Query<Note>() 
        .Where(x => x.EntityId == entity.Id && 
           x.EntityType == entityType) 
        .ToList(); 
} 

public void AddNote(IEntity entity, Note note) 
{ 
    note.EntityType = ObjectContext.GetObjectType(entity.GetType()).Name; 
    note.EntityId = entity.Id; 
    context.Add(note); 
} 
: 여기

은 단순 발췌입니다
1

당신은 주 건물 (. ES WithComment) 지도를 새 기본 클래스로 모든 엔티티를 확장 할 수 있습니다 당신은 새 속성 매핑 할 클래스를 작성할 수있는 것보다 :

public class WithComment 
{ 
    public virtual IList<Note> Notes { get; set; } 
} 

public class Customer : WithComment 
{ 
} 

public class CustomerMapping : EntityTypeConfiguration<Customer> 
{ 
     public CustomerMapping() 
     { 
      ToTable("Customers"); 
      .......................... 

      HasMany<Notes>(o => o.Notes); // relation to note class 
     } 
} 

을 다음

구성 추가
public class MyContext : DbContext 
{ 
     public MyContext() { } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Configurations.Add(new CustomerMapping()); 

      base.OnModelCreating(modelBuilder); 
     } 
}