2013-06-18 3 views
2

NHibernate 3.3.3을 사용하여 NHibernate 매핑 By-code로 FluentNHibernate 매핑을 변환하려고합니다. 목표는 NHibernate 3.3.3으로 업그레이드하고 배포되는 어셈블리의 수를 줄이는 것입니다.매핑 NHibernate 다중 열 ManyToOne 매핑

그러나 나는 컴파일 할 때와 실행 나는 다음과 같은 예외 얻을 :

NHibernate.MappingException : 다중 열 속성이 단일 열 API를 통해 매핑 할 수 없습니다.

는 XML 매핑 FluentNHibernate은 다음과 같이 내 외모를 가져옵니다

References(x => x.Name) 
    .Columns("NameTextId", "LanguageId") 
    .Cascade.None() 
    .Fetch.Join() 
    .NotFound.Ignore() 
    .Not.Unique() 
    .Not.LazyLoad(); 
:

this.ManyToOne(u => u.Name, c => 
{ 
    c.Cascade(Cascade.None); 
    c.Class(typeof(TextDto)); 
    c.Columns(
     x => 
     { 
      x.Name("NameTextId"); 
      x.Unique(false); 
     }, 
     x => 
     { 
      x.Name("LanguageId"); 
      x.Unique(false); 
     }); 
    c.Fetch(FetchKind.Join); 
    c.Lazy(LazyRelation.NoLazy); 
    c.NotFound(NotFoundMode.Ignore); 
    c.Unique(false); 
}); 

이 이전 FluentNHibernate 매핑 :

여기
<many-to-one cascade="none" class="TextDto" fetch="join" lazy="false" name="Name" not-found="ignore"> 
    <column name="NameTextId" unique="false" /> 
    <column name="LanguageId" unique="false" /> 
</many-to-one> 

나의 새로운 바이 코드 매핑

완전성 속성 유형은 invol VED :

public class TextDto 
{ 
    public TextCompositeId Id { get; set; } 
    public string PluralText { get; set; } 
    public string SingularText { get; set; } 
    public override bool Equals(object obj) 
    { 
     var text = (TextDto)obj; 
     if (text == null) return false; 
     return this.Id.Equals(text.Id); 
    } 
    public override int GetHashCode() 
    { 
     return this.Id.GetHashCode(); 
    } 
} 

그리고 기업에서 속성의 예 :

public class CharacteristicValue 
{ 
    public CharacteristicValueCompositeId Id { get; set; } 
    public TextDto Name { get; set; } 
    public string LanguageIdentity { get; set; } 
    public string Value 
    { 
     get 
     { 
      string value = null; 
      if (this.ValueMultilingual != null) return this.ValueMultilingual.SingularText; 
      else if (!string.IsNullOrEmpty(this.ValueMeta)) return this.ValueMeta; 
      return value; 
     } 
    } 
    public TextDto ValueMultilingual { get; set; } 
    public string ValueMeta { get; set; } 
    public override bool Equals(object obj) 
    { 
     if (obj == null) return false; 
     if (object.ReferenceEquals(this, obj)) return true; 
     CharacteristicValue characteristicValue = obj as CharacteristicValue; 
     if (characteristicValue == null) return false; 
     if (this.Id != characteristicValue.Id) return false; 
     return true; 
    } 
    public override int GetHashCode() 
    { 
     return this.Id.GetHashCode(); 
    } 
} 

그래서, 어떻게 내가 FluentNHibernate에 도착하는 데 사용되는 XML 매핑을 얻을 수 있지만-코드 NHiberbate의 매핑합니까?

답변

2

매핑에서 ManyToOne 매핑에서 c.Unique(false);을 제거하십시오. 이 설정은 이제 각 열에 적용됩니다.

this.ManyToOne(u => u.Name, c => 
{ 
    ... // the same as above 

    // c.Unique(false); // it is setting now related to columns 
}); 

그리고 당신이 열 중 하나에 고유성을 변경할 경우에는

<many-to-one name="Name" class="TextDto" fetch="join" lazy="false" not-found="ignore"> 
    <column name="NameTextId" unique="true" /> 
    <column name="LanguageId" /> 
</many-to-one> 

을받을 것이다 : 고유 제한 조건은 해당 열을 추가 할 것입니다

x => 
{ 
    x.Name("NameTextId"); 
    x.Unique(true); // change here 
}, 

:

<column name="NameTextId" unique="true" /> 
+0

감사합니다. 나에게 매핑이 필요합니다. 에드. 또한 여기 c.NotNullable (false)도 사용할 수 없다는 것을 알았습니다. – Wietze

+0

그게 도움이된다면! ;) –