How to map uint in NHibernate with SQL Server 2005

前端 未结 4 1891
生来不讨喜
生来不讨喜 2021-01-19 01:42

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 t

相关标签:
4条回答
  • 2021-01-19 01:54

    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"/>
    
    0 讨论(0)
  • 2021-01-19 02:05
    <property name="Prop" type="long"/>
    
    0 讨论(0)
  • 2021-01-19 02:09

    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>
    
    0 讨论(0)
  • 2021-01-19 02:13

    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.

    0 讨论(0)
提交回复
热议问题