1

I'm trying to use a convention to map UInt32 properties to a SQL Server 2008 database. I don't seem to be able to create a solution based on existing web sources, due to updates in the way Fluent NHibernate works - i.e. examples are out of date.

I'm trying to have NHibernate generate the schema (via ExposeConfiguration). I'm happy to have NHibernate map it to anything sensible (e.g. bigint).

Here's my code as it currently stands (which, when I try to expose the schema, fails due to SQL Server not supporting UInt32). Apologies for the code being a little long, but I'm not 100% sure what is relevant to the problem, so I'm erring on the side of caution. Most of it is based on this post.

The error reported is:

System.ArgumentException : Dialect does not support DbType.UInt32

I think I'll need a relatively comprehensive example, as I don't seem to be able to pull the pieces together into a working solution, at present.

FluentConfiguration configuration =
    Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008
            .ConnectionString(connectionString))
        .Mappings(mapping =>
            mapping.AutoMappings.Add(
                AutoMap.AssemblyOf<Product>()
                    .Conventions.Add<UInt32UserTypeConvention>()));

configuration.ExposeConfiguration(x => new SchemaExport(x).Create(false, true));

namespace NHibernateTest
{
    public class UInt32UserTypeConvention : UserTypeConvention<UInt32UserType> 
    {
        // Empty.
    }
}

namespace NHibernateTest
{
    public class UInt32UserType : IUserType
    {
        // Public properties.

        public bool IsMutable
        {
            get
            {
                return false;
            }
        }

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

        public SqlType[] SqlTypes
        {
            get
            {
                return 
                    new SqlType[] 
                    { 
                        SqlTypeFactory.Int32 
                    };
            }
        }

        // Public methods.

        public object Assemble(object cached, object owner)
        {
            return cached;
        }

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

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

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

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

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

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

        public object Replace(object original, object target, object owner)
        {
            return original;
        }
    }
}
Community
  • 1
  • 1
dommer
  • 19,610
  • 14
  • 75
  • 137
  • possible duplicate of [Best way to store UInt32 in Sql Server](http://stackoverflow.com/questions/694211/best-way-to-store-uint32-in-sql-server) – gbn May 13 '10 at 14:08
  • 1
    @gbn. This is primarily a Fluent NHibernate question, so definitely not addressed by the post you refer to. I want to know how to create a Fluent NHibernate mapping when I have a UInt32 property. I'm comfortable with the options for casting from UInt32 to other numeric types. – dommer May 13 '10 at 14:28
  • See my answer then. It may be a Fluent NHibernate question but the issue is how to map Unint32 to a SQL Server datatype... which could be any client or ORM... – gbn May 13 '10 at 15:18
  • Could you describe your error in more detail? The code you've posted works for me. NHibernate generates a database with int columns for the UInt32 values. – Erik Öjebo May 13 '10 at 18:02
  • @Erik. The error is "Dialect does not support DbType.UInt32". I've added it to the main question too. – dommer May 13 '10 at 23:51
  • @dommer What version of NHibernate are you using? I've tried it successfully with both 2.1.0.4 and 2.1.2.4. – Erik Öjebo May 14 '10 at 09:28

1 Answers1

0

You'd need to map to an existing SQL Server datatype, of course.

Based on this question, "Best way to store UInt32 in Sql Server", your choices:

  • CLR datatype
  • bigint
  • decimal
  • a workaround using int.MinValue to map to int
Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
  • Thanks for the response. What I'm struggling with is how to have the Fluent NHibernate automapper create the schema when there's a UInt32 property. I'm happy for it to be mapped to a bigint (or an int, as I know it's always going to be a smallish value) in SQL Server. However, the automapping chokes when there's a UInt32 property. – dommer May 13 '10 at 15:39