我有以下示例持久化类:
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中的所有内容对我来说都是正确的......任何想法我都缺少了吗?
答案 0 :(得分:1)
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"...