我正在将一个简单的工作演示从nhibernate移植到流畅。我现有的nhibernate映射是这样的:
<?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");
}
}
但我收到错误:
---&GT; NHibernate.MappingException:属性映射的编号错误 列:MoneyHibernate.Invoice.TotalValue类型: MoneyHibernate.MoneyCompositeUserType
所以我认为两次声明列不是正确的方法吗?
答案 0 :(得分:6)
您正在以正确的方式执行此操作,但是,您需要在手动声明列之前将Columns.Clear()
添加到映射中,如下所示:
Map(x => x.TotalValue)
.CustomType(typeof (MoneyCompositeUserType))
.Columns.Clear()
.Columns.Add("TotalValue_Amount", "TotalValue_Amount");
否则,除了复合用户类型映射的列集之外,nHibernate还会追加新的列名(因此错误的列数异常)。