gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:10 26 4
gpt4 key购买 nike

我有以下示例持久类:

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"...

关于c# - NHibernate 中的多对一映射不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32915143/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com