如何映射一个ICompositeUserType [英] How do I map an ICompositeUserType
问题描述
我从移植到NHibernate的流畅简单的工作演示。我现有的NHibernate的映射是这样的:
I am porting a simple working demo from nhibernate to fluent. My existing nhibernate mapping is this:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MoneyHibernate"
namespace="MoneyHibernate">
<class name="Invoice" table="Invoices">
<id name="Id">
<generator class="guid"/>
</id>
<property name="Number"/>
<property name="Customer"/>
<property name="TotalValue" type="MoneyHibernate.MoneyCompositeUserType,MoneyHibernate">
<column name="TotalValue_Amount" not-null="true" />
<column name="TotalValue_Currency" length="3" not-null="true" />
</property>
</class>
</hibernate-mapping>
我试图创造equlivilant ClassMap
:
internal class InvoiceMap : ClassMap<Invoice>
{
public InvoiceMap()
{
Id(x => x.Id);
Map(x => x.Customer);
Map(x => x.Number);
Map(x => x.TotalValue)
.CustomType(typeof (MoneyCompositeUserType))
.Column("TotalValue_Amount")
.Column("TotalValue_Currency");
}
}
但我得到的错误:
But I get the error:
---> NHibernate.MappingException:属性映射有列错号
:MoneyHibernate.Invoice.TotalValue类型:
MoneyHibernate。 MoneyCompositeUserType
---> NHibernate.MappingException: property mapping has wrong number of columns: MoneyHibernate.Invoice.TotalValue type: MoneyHibernate.MoneyCompositeUserType
所以我推测宣布柱两次,是不是这样做的正确方法?
So I presume that declaring column twice is not the correct way to do this?
推荐答案
您在本做了正确的方式,但是,你需要添加 Columns.Clear()
来你的映射之前,像这样的列的手册报关:
You are doing this the right way, however, you need to add Columns.Clear()
to your mapping prior to the manual declaration of the columns like this:
Map(x => x.TotalValue)
.CustomType(typeof (MoneyCompositeUserType))
.Columns.Clear()
.Columns.Add("TotalValue_Amount", "TotalValue_Amount");
,否则的nHibernate将除了列集合的复合用户类型映射追加新的列名(因此,错号的列除外)。
Otherwise nHibernate will append the new column names in addition to the column collection for your composite user type mapping (hence the wrong number of columns exception).
这篇关于如何映射一个ICompositeUserType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!