How to map uint in NHibernate with SQL Server 2005
Asked Answered
M

4

5

I have a property of type uint on my entity. Something like:

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

When I try to persist that into the SQL Server 2005 database, I get an exception

Dialect does not support DbType.UInt32

What would be the easiest way to workaround this. I could for example store it as long in the DB. I only don't know how to tell that to NHibernate.

Midkiff answered 12/3, 2009 at 16:25 Comment(0)
G
4

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

Take an example, like this one and adapt it. If you have many uint's, it is worth to have a user type.

<property name="Prop" type="UIntUserType"/>
Gailgaile answered 27/5, 2009 at 9:10 Comment(1)
Yeah, that's what I ultimately ended up doing. Thanks for help guys, all of you.Midkiff
L
2

Haven't tried this so not sure if this will work for you but you could try creating your own Dialect and registering that in the web.config/app.config

Dialect class:

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>
Lighthearted answered 21/5, 2009 at 5:19 Comment(0)
M
1
<property name="Prop" type="long"/>
Maxillary answered 12/3, 2009 at 16:43 Comment(6)
Then may be you should describe the problem a little betterMaxillary
This did work for me... with type="long" SchemaExport exports the field as BIGINTLasley
what version of NHibernate do you use?Midkiff
NH 2.0.1GA... are you getting the same exception after defining type="long"?Lasley
just tested this, seems to work... code.google.com/p/mausch/source/browse/trunk/…Lasley
It does work for save, but throws a cast exception during retrieve, due to quirky bahavior of CLR and what I'd call a bug in NH. It tries to unbox object holding long into an uint, which throws InvalidCastException, because CLR does not perform numeric conversions during unboxing.Midkiff
G
0

You could try to add another private "mirror"-property.

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 do this only if it could not be solved by the mapping.

Gailgaile answered 20/5, 2009 at 9:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.