如何创建/使用 Fluent NHibernate 约定将 UInt32 属性自动映射到 SQL Server 2008 数据库?

发布于 2024-09-01 09:49:36 字数 2928 浏览 3 评论 0原文

我正在尝试使用约定将 UInt32 属性映射到 SQL Server 2008 数据库。由于 Fluent NHibernate 工作方式的更新,我似乎无法基于现有的 Web 源创建解决方案 - 即示例已过时。

我试图让 NHibernate 生成架构(通过 ExposeConfiguration)。我很高兴 NHibernate 将其映射到任何合理的东西(例如 bigint)。

这是我当前的代码(当我尝试公开架构时,由于 SQL Server 不支持 UInt32,该代码失败)。对于代码有点长表示歉意,但我不能 100% 确定与问题相关的内容,所以我谨慎行事。其中大部分内容基于这篇文章

报告的错误是:

System.ArgumentException : Dialect does not support DbType.UInt32

我认为我需要一个相对全面的示例,因为目前我似乎无法将各个部分组合成一个可行的解决方案。

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;
        }
    }
}

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;
        }
    }
}

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

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

发布评论

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

评论(1

蓝天 2024-09-08 09:49:36

当然,您需要映射到现有的 SQL Server 数据类型。

基于这个问题,“在Sql Server中存储UInt32的最佳方法”< /a>,您的选择:

  • CLR 数据类型
  • bigint
  • decimal
  • 使用 int.MinValue 映射到 int 的解决方法

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