如何使用 SQL Server 2005 映射 NHibernate 中的 uint

发布于 2024-07-15 03:28:56 字数 296 浏览 14 评论 0原文

我的实体有一个 uint 类型的属性。 类似于:

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

当我尝试将其持久保存到 SQL Server 2005 数据库中时,出现异常

方言不支持 DbType.UInt32

解决此问题的最简单方法是什么。 例如,我可以将它存储在数据库中很长时间。 我只是不知道如何将其告诉 NHibernate。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吃兔兔 2024-07-22 03:28:56

最干净、最官方的解决方案可能是编写一个用户类型。

举个例子,比如 这个并进行调整。 如果您有许多uint,那么有一个用户类型是值得的。

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

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"/>
满栀 2024-07-22 03:28:56

还没有尝试过这个,所以不确定这是否适合你,但你可以尝试创建自己的方言并在 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>

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>
简美 2024-07-22 03:28:56
<property name="Prop" type="long"/>
<property name="Prop" type="long"/>
黑凤梨 2024-07-22 03:28:56

您可以尝试添加另一个私有“镜像”属性。

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"/>

当然,如果映射无法解决该问题,则应执行此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文