NHibernate多列ManyToOne映射与映射代码



我正试图将我的FluentNHibernate映射转换为使用NHibernate 3.3.3的NHibernate映射代码。我们的目标是升级到NHibernate 3.3.3,并减少正在分发的程序集的数量。

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

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>

这是我新的By-Code映射:

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();

对于完整性,涉及的属性类型:

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,但与nhibernate的映射代码?

在您的映射中,从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" />

最新更新