0

I am trying to support UInt32 in my application.By looking example given at this location,i wrote my code. NHibernate - How to store UInt32 in database. In my hbm file i defined property tag:

           <property name="Uint16Var" column="Uint16Var" 
           type="datatypeSupported.UInt32Type,  datatypeSupported"  />

and i also defined a UInt32Type class like this:

     using System;
     using NHibernate;
     using NHibernate.SqlTypes;
     using NHibernate.UserTypes;

    namespace datatypeSupported
    {
     public class UInt32Type : IUserType
    {

    public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
    {
        int? i = (int?)NHibernateUtil.Int32.NullSafeGet(rs, names[0]);
        return (UInt32?)i;
    }

    public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
    {
        UInt32? u = (UInt32?)value;
        int? i = (Int32?)u;
        NHibernateUtil.Int32.NullSafeSet(cmd, i, index);  
    }

    public Type ReturnedType
    {
        get
        {
            return typeof(Nullable<UInt32>);
        }
    }

    public SqlType[] SqlTypes
    {
        get
        {
            return new SqlType[] { SqlTypeFactory.Int32 };
        }
    } public object Assemble(object cached, object owner) 
    { 
        return cached; 
    } 

    public object DeepCopy(object value)
    { 
        return value;
    } 

    public object Disassemble(object value) 
    {
        return value; 
    } 

    public int GetHashCode(object x) 
    { 
        return x.GetHashCode(); 
    }

    public bool IsMutable
    {
        get { return false; }
    } 

    public object Replace(object original, object target, object owner) 
    { 
        return original; 
    } 

    public new bool Equals(object x, object y) 
    { 
        return x != null && x.Equals(y);
    }
}

}

but still when i try to save my entity, it gives a error "Dialect does not support DbType.UInt32".What type of changes i required to do ?

Community
  • 1
  • 1

1 Answers1

2

Write a custom dialect that adds support to UInt32 type. Depending on the database you are using, inherit from one of the known dialects. I.e., for SQL Server, you could have something like this:

public class CustomMsSqlDialect : MsSql2008Dialect
{
    protected override void RegisterNumericTypeMappings()
    {
        base.RegisterNumericTypeMappings();
        RegisterColumnType(DbType.UInt32, "INT");
    }
}

To register the dialect with FluentNHibernate:

Fluently.Configure().Database(
    MsSqlConfiguration.MsSql2008.Dialect<CustomMsSqlDialect>()...)

And with XML:

<property name="dialect">CustomMsSqlDialect, AssemblyName</property>
Miroslav Popovic
  • 12,100
  • 2
  • 35
  • 47
  • Thanks sir.I want to implement in ms Sql server 2008, but i am not able to find any suitable or similar method like RegisterNumericTypeMappings() in msSql2008 dialect to override. Can u please help me with SqlServer2008 dialect. – rajesh Kumar Jul 23 '12 at 08:35
  • Actually, it's in `MsSql2000Dialect` class, that `MsSql2008Dialect` class inherits from: https://github.com/nhibernate/nhibernate-core/blob/master/src/NHibernate/Dialect/MsSql2000Dialect.cs You should be able to override, I just checked with latest NHibernate version. – Miroslav Popovic Jul 23 '12 at 08:42
  • @ Miroslav Popovic: still i am getting the same error.I can't find any method.I am using hibernate (Version: 3.2.0.4000). – rajesh Kumar Jul 23 '12 at 08:59
  • @rajeshKumar Make sure you are inheriting from the right class (`MsSql2008Dialect`) and that your method is named correctly (`RegisterNumericTypeMappings`). Just checked the older NHibernate version source - 2.1, and it looks the same there. – Miroslav Popovic Jul 23 '12 at 09:10
  • @ Miroslav Popovic: Sry sir...But i just copied paste your code..as given above..But still i am getting the same error-"no suitable method found to override".I checked all interface definition but still i am not able to find that method.In Dialect interface i can find this method - protected internal void RegisterHibernateType(DbType code, string name),but not RegisterNumericTypeMappings(). Please help me. – rajesh Kumar Jul 23 '12 at 09:47
  • @rajeshKumar Are you sure you are inheriting from `MsSql2008Dialect` class? You can also use `RegisterColumnType` in your CustomSqlDialect constructor instead of overriding. See KaraT's answer here: http://stackoverflow.com/a/891481/119230 – Miroslav Popovic Jul 23 '12 at 10:17