How to map uint in NHibernate to SQL Server 2005

I have a uint type property for my object. Sort of:

public class Enity
{
   public uint Count {get;set;}
}

When I try to save this in a SQL Server 2005 database, I get an exception

Dialect does not support DbType.UInt32

What would be the easiest way around this. I could, for example, store it longer in the database. I just don't know how to report this to NHibernate.

+5
source share
4 answers

The cleanest, most formal solution would probably be to write a user type.

Take an example, for example this one and adapt it. If you have a lot uint, it is worth specifying the type of user.

<property name="Prop" type="UIntUserType"/>
+4

, , , Dialect web.config/app.config

:

public class MyDialect:MsSql2005Dialect
{
    public MyDialect()
    {            
        RegisterColumnType(System.Data.DbType.UInt32, "bigint");            
    }
}

Web.config:

configuration>
 <configSections>
  <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
 </configSections>

                <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
   <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
   <property name="connection.connection_string">
    Server=127.0.0.1; Initial Catalog=thedatabase; Integrated Security=SSPI
   </property>
   <property name="dialect">MyDialect</property>
   <property name="current_session_context_class">managed_web</property>
  </session-factory>
 </hibernate-configuration>
    <!-- other app specific config follows -->

</configuration>
+2
<property name="Prop" type="long"/>
+1
source

You can try to add one more private property "mirror".

public class Enity
{
   public uint Count {get;set;}

   private long CountAsLong 
   { 
     get { return Convert.ToInt64(Count); } 
     set { Count = Convert.ToUInt(value); }
   }
}

<property name="CountAsLong" type="long"/>

Of course, you should only do this if it cannot be resolved by matching.

0
source

All Articles