How do I map an ICompositeUserType

点点圈 提交于 2019-12-19 21:46:54

问题


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>

I have tried to create 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: 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?


回答1:


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");

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



来源:https://stackoverflow.com/questions/16935161/how-do-i-map-an-icompositeusertype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!