NHibernate ComponentAsId() 实际上并没有设置 Id 属性
我正在尝试使用编码映射设置 NHibernate (3.2.0.4000),但我无法让 ComponentAsId 正常工作。
我有一个数据库模式,我无法更改主键由字符串和日期组成的位置,因此我需要使用组件作为 Id:
即
数据库模式
Id varchar(20) NOT NULL,
EffectiveDate datetime NOT NULL,
Property1 int
类
public class EffectiveDateId
{
public string Id { get; set; }
public DateTimeOffset EffectiveDate { get; set; }
}
public class MyClass
{
public EffectiveDateId Identity { get; set; }
public int Property1 { get; set; }
}
映射
var mapper = new ConventionModelMapper();
mapper.Class<MyClass>(map => map.ComponentAsId(id => id.Identity, cid =>
{
cid.Property(x => x.Id, x => x.Length(20));
cid.Property(x => x.EffectiveDate);
}));
但这不起作用,因为 MyClass 上的 Identity 属性是无效的。
XML 的映射如下所示:
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.MyClass, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<composite-id class="EffectiveDateId, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key-property name="Id" type="AnsiString" length="20" />
<key-property name="EffectiveDate" />
</composite-id>
<property name="Property1" />
</class>
</hibernate-mapping>
但是,如果我手动更改 XML 以包含 name="Identity" ,如下所示:
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.MyClass, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<composite-id name="Identity" class="EffectiveDateId, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key-property name="Id" type="AnsiString" length="20" />
<key-property name="EffectiveDate" />
</composite-id>
<property name="Property1" />
</class>
</hibernate-mapping>
它可以工作。
我尝试通过添加(除了 ComponentAsId 映射之外)来设置 id,
mapper.Class<MyClass>(map => map.Id(id => id.Identity));
但这只会生成 NHibernate.MappingException
Test.MyClass id 的映射不明确。定义了带有生成器的 id 属性,并且您正在尝试将属性“Identity”的组件 Test.EffectiveDateId 映射为 Test.MyClass 的 id。
使用调试器,我可以看到对于代码映射配置,ClassMapping.IdentifierProperty 为 null,而在 XML 配置中,它设置为 NHibernate.Mapping.Property 实例。
那么,ComponentAsId() 的实现中是否存在未设置 IdentifierProperty 的错误,或者我做错了?
问候, 埃蒙
I am trying to set up NHibernate (3.2.0.4000) using the coded mapping but I can't get the ComponentAsId working properly.
I have a database schema that I can't change where the primary key consists of a string and a date so I need to use a component as the Id:
i.e.
Database schema
Id varchar(20) NOT NULL,
EffectiveDate datetime NOT NULL,
Property1 int
Classes
public class EffectiveDateId
{
public string Id { get; set; }
public DateTimeOffset EffectiveDate { get; set; }
}
public class MyClass
{
public EffectiveDateId Identity { get; set; }
public int Property1 { get; set; }
}
Mapping
var mapper = new ConventionModelMapper();
mapper.Class<MyClass>(map => map.ComponentAsId(id => id.Identity, cid =>
{
cid.Property(x => x.Id, x => x.Length(20));
cid.Property(x => x.EffectiveDate);
}));
But this does not work as the Identity propery on MyClass is null.
The mapping as XML looks like this:
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.MyClass, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<composite-id class="EffectiveDateId, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key-property name="Id" type="AnsiString" length="20" />
<key-property name="EffectiveDate" />
</composite-id>
<property name="Property1" />
</class>
</hibernate-mapping>
However if I manually change the XML to include name="Identity" like this:
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2">
<class name="Test.MyClass, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<composite-id name="Identity" class="EffectiveDateId, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key-property name="Id" type="AnsiString" length="20" />
<key-property name="EffectiveDate" />
</composite-id>
<property name="Property1" />
</class>
</hibernate-mapping>
it works.
I've tried setting the id by adding (in addition to the ComponentAsId mapping)
mapper.Class<MyClass>(map => map.Id(id => id.Identity));
but that just generates an NHibernate.MappingException
Ambiguous mapping of Test.MyClass id. An id property, with generator, was defined and you are trying to map the component Test.EffectiveDateId, of property 'Identity', as id for Test.MyClass.
Using the debugger I can see that for the code mapped configuration, the ClassMapping.IdentifierProperty is null while in the XML confguration it is set to a NHibernate.Mapping.Property instance.
So, is there a bug in the implementation of ComponentAsId() that is not setting the IdentifierProperty or am I doing it wrong?
regards,
Eamon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论