2012-01-23 2 views
0

나는 다음과 같은 계층 개체를 매핑을 시도하고, NHibernate에 3.2 ByCode 구성을 사용 ByCode이 잘 작동계층 엔티티 부모 키

public class BusinessTypeMapper : ClassMapping<BusinessType> 
{ 
    public BusinessTypeMapper() 
    { 
     Id(x => x.BusinessTypeId, x => x.Type(new GuidType())); 
     Property(x => x.ParentBusinessTypeId, x => x.Type(new GuidType())); 
     Property(x => x.BusinessTypeName); 
     Set(x => x.Children, 
      cm => 
       { 
        // This works, but there is an ugly string in here 
        cm.Key(y => y.Column("ParentBusinessTypeId")); 
        cm.Inverse(true); 
        cm.OrderBy(bt => bt.BusinessTypeName); 
        cm.Lazy(CollectionLazy.NoLazy); 
       }, 
      m => m.OneToMany()); 
    } 
} 

,하지만 난 차라리 람다를 사용하여 릴레이션의 핵심을 지정하여 리팩토링이 작동하도록 할 수 있습니다. 이것은 다음과 같이 사용할 것 같다 :

public class BusinessTypeMapper : ClassMapping<BusinessType> 
{ 
    public BusinessTypeMapper() 
    { 
     Id(x => x.BusinessTypeId, x => x.Type(new GuidType())); 
     Property(x => x.ParentBusinessTypeId, x => x.Type(new GuidType())); 
     Property(x => x.BusinessTypeName); 
     Set(x => x.Children, 
      cm => 
       { 
        // This compiles and runs, but generates some other column 
        cm.Key(y => y.PropertyRef(bt => bt.ParentBusinessTypeId)); 
        cm.Inverse(true); 
        cm.OrderBy(bt => bt.BusinessTypeName); 
        cm.Lazy(CollectionLazy.NoLazy); 
       }, 
      m => m.OneToMany()); 
    } 
} 

문제는이 이미 구성 ParentBusinessTypeId을 무시하고, businesstype_key라는 열을 생성하는 NHibernate에 원인이 있다는 것입니다. Hibernate가 문자열이 아닌 관계를 지정하기 위해 람다 (lambda)를 사용하도록하는 방법이 있습니까?

+0

'public virtual BusinessType Parent {get; 세트; }'? – Firo

+0

내 질문에 그 문제를 해결하는 데 도움이 될까요? 나는 부모에게서 자식까지 탐색 할 필요가 없으므로 필요 없다고 생각했다. –

답변

1

는 난 단지 부모가 아이들에게 에서, 부모는 어린이의 탐색 할 필요가 없습니다, 그래서 그것을 필요

다음 completly public virtual Guid? ParentBusinessTypeId { get; set; }을 제거 생각하지 않았다. 그런 다음 NH은 "businesstype_key"(컨벤션) 만 만들고 "ParentBusinessTypeId"는 만들지 않습니다. 그것을 바꾸고 싶다면 선호하는 컬럼 이름을 cm.Key(y => y.Column("yourpreferredColumnName"));으로 지정해야한다.

+0

좋아, 나는 그것을 살 수있다. 비록 다른 모든 컬럼에 대한 프로퍼티 이름을 사용하기 때문에 어리석은 컨벤션으로 보이기 때문에 실제로는 일관성없는 스키마가됩니다. 호기심에서 부모 속성을 매핑하면 원래 의도 한 결과가 가능합니까? 또한 PropertyRef는 어떤 용도로 사용됩니까? –

+0

"부모 속성을 매핑하는 경우"-> 아마 대부분은 아닐 것입니다. 규칙은 동일하다고 생각합니다. "PropertyRef"->는 외래 키가 PrimaryKey를 참조하지 않지만 다른 속성을 참조 할 때 사용됩니다. 'clientname -> client.name' 대신'client_id -> client.id' – Firo

+0

매우 감사합니다, @Firo! –

관련 문제