如何在NHibernate中将uint映射为SQL Server 2005

时间:2021-10-14 11:23:27

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

我的实体上有一个类型为uint的属性。喜欢的东西:

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

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

当我试图将它持久化到SQL Server 2005数据库中时,我得到了一个异常

Dialect does not support DbType.UInt32

方言不支持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.

解决这个问题最简单的方法是什么?我可以把它存储在DB中。我只是不知道怎么告诉NHibernate。

4 个解决方案

#1


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.

举个例子,就像这个,把它改编一下。如果您有许多uint类型,那么拥有一个用户类型是值得的。

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

#2


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

还没有尝试过,所以不确定这是否对你有用,但是你可以尝试创建自己的方言并在web.config/app.config中注册它。

Dialect class:

方言类:

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

Web.config:

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

#3


1  

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

#4


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.

当然,只有在映射不能解决时,才应该这样做。

#1


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.

举个例子,就像这个,把它改编一下。如果您有许多uint类型,那么拥有一个用户类型是值得的。

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

#2


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

还没有尝试过,所以不确定这是否对你有用,但是你可以尝试创建自己的方言并在web.config/app.config中注册它。

Dialect class:

方言类:

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

Web.config:

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

#3


1  

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

#4


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.

当然,只有在映射不能解决时,才应该这样做。