NHibernate 中的多对一映射不起作用?

Many-to-One Mapping in NHibernate Not Working?

我有以下示例持久化类:

using NHibernate.Mapping.Attributes;

namespace GumiDAL.Domain
{
    [Class]
    public class Foo
    {
        [Id(0)]
        [Generator(1, Class="identity")]
        public virtual int Id { get; set; }

        [Property]
        public virtual string Name { get; set; }

    }

    [Class]
    public class Bar
    {
        [Id(0)]
        [Generator(1, Class = "identity")]
        public virtual int Id { get; set; }


        [ManyToOne(Name="foo")]
        public virtual Foo foo { get; set; }
    }

}

序列化程序集会创建以下 xml:

<!--
Generated from NHibernate.Mapping.Attributes on 2015-10-02 13:08:49Z.
-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="Example.Domain.Foo, ExampleAssembly">
        <id>
            <generator class="identity"/>
        </id>
        <property name="Name"/>
    </class>
    <class name="Example.Domain.Bar, ExampleAssembly">
        <id>
            <generator class="identity"/>
        </id>
        <many-to-one name="foo"/>
    </class>
</hibernate-mapping>

但是当我尝试设置配置时,出现异常并显示以下消息和堆栈跟踪:

Could not compile deserialized mapping document.

A first chance exception of type 'NHibernate.MappingException' occurred in NHibernate.dll
   at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 344
   at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 532
   at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 501
   at NHibernate.Cfg.Configuration.ProcessMappingsQueue() in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1867
   at NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1858
   at NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 1851
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 640
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream) in c:\Projects\nhibernate-core\src\NHibernate\Cfg\Configuration.cs:line 614
   at Example.Domain.Tests.SetUp()

对我来说,xml 中的一切看起来都是正确的...知道我遗漏了什么吗?

name="" 属性表示 C# 属性。所以,我们应该使用

// instead of this
<many-to-one name="foo_id"/>
// we need this
<many-to-one name="foo" column="foo_id"/>

因此,属性 的名称是 foo 而不是 foo_id。我想我们需要这样的映射:

//[ManyToOne(Name="foo_id")]
[ManyToOne(Column="foo_id")]
public virtual Foo foo { get; set; }

有了完整的堆栈异常会更容易,而且,<id> 元素应该有名称:

<id name="Id" column="foo_id"...