2013-06-18 202 views
2

我想我FluentNHibernate映射转换为NHibernate的映射关系通过代码使用NHibernate 3.3.3。目标是升级到NHibernate 3.3.3并减少正在分发的程序集数量。NHibernate的多列多对一映射与映射到码

然而,当我编译和运行我得到以下异常:

NHibernate.MappingException:多列属性不能被通过单列API映射。

XML映射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> 

这是我的新的副代码映射:

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映射:

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

出于完整性属性类型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(); 
    } 
} 

所以,我怎么获得XML映射我用来获取与FluentNHibernate但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

伟大的,如果帮助! ;) –