【问题标题】:How to map uint in NHibernate with SQL Server 2005如何使用 SQL Server 2005 在 NHibernate 中映射 uint
【发布时间】:2009-03-12 16:25:59
【问题描述】:

我的实体上有一个 uint 类型的属性。比如:

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

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

方言不支持 DbType.UInt32

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

【问题讨论】:

    标签: .net sql-server-2005 nhibernate orm uint32


    【解决方案1】:

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

    举个例子,比如this one 并加以调整。如果你有很多uint,那么值得拥有一个用户类型。

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

    【讨论】:

    • 是的,这就是我最终所做的。谢谢大家的帮助。
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:
      <property name="Prop" type="long"/>
      

      【讨论】:

      • 那么你可能应该更好地描述问题
      • 确实为我工作...使用 type="long" SchemaExport 将字段导出为 BIGINT
      • 您使用什么版本的 NHibernate?
      • NH 2.0.1GA...在定义 type="long" 后你是否得到同样的异常?
      • 刚刚测试过这个,似乎可以工作...code.google.com/p/mausch/source/browse/trunk/…
      【解决方案4】:

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

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

      当然,您应该在映射无法解决的情况下这样做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 2011-03-09
        相关资源
        最近更新 更多