NHibernate中的SQL 2008 HierarchyID支持 [英] SQL 2008 HierarchyID support in NHibernate

查看:100
本文介绍了NHibernate中的SQL 2008 HierarchyID支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索了各种NHibernate列表,但没有给出明确的答案. SQL2008方言不会'似乎支持HierarchyID数据类型-仅适用于新的日期和时间类型.

Searched various NHibernate lists and haven't come up with a definitive answer. The SQL2008 dialect doesn't appear to have support for the HierarchyID data type - new date and time types only.

有人有好的实施或有效的解决方法吗?我真的很想在我的新应用中利用HierarchyID. MS自己的工具中非常缺少对这种有趣而强大的数据类型的支持,因此,对于NHibernate不提供支持,我并不感到震惊.

Does anyone have a good implementation or an effective workaround? I'd really like to leverage HierarchyID in a new app of mine. Support for this interesting and powerful data type is sorely lacking in MS's own tools so I'm not shocked that NHibernate doesn't have support.

一些 方法.想知道是否有人对什么有用,什么更有效等有一些经验."

There are some approaches out there that I haven't delved into yet. Wondering if anyone has some experience in what works, what is more performant, etc.'

完全公开:我正在使用Castle ActiveRecord,但这似乎是NHibernate问题.

Full disclosure: I'm working with Castle ActiveRecord but this seems like an NHibernate issue.

推荐答案

免责声明:我不是NHibernate专家,但是,在即将使用SQL Server 2008 R2和层次结构ID的项目中,我们将它与Fluent一起使用.下面的代码是我们当前在开发环境中使用的代码,尚未经过全面测试/完善.我从其他地方复制了大部分代码(对不起,我失去了链接!)

Disclaimer: Im not an NHibernate expert, however, we are using it with Fluent in an upcoming project which uses SQL Server 2008 R2 and Hierarchy IDs. The code below is what we are using currently on our dev environment and is not fully tested/refined. I copied the majority of the code from elsewhere (sorry I lost the link!)

您需要创建一个用户定义的类型,然后在映射中使用它.下面的映射是Fluent,我不知道如何使用ActiveRecord进行操作,但我想它应该是相似的!

You need to create a User Defined Type and then use it in your mappings. The mapping below is Fluent, Im not aware how to do it using ActiveRecord but Im guessing it should be similar!

用户定义类型

namespace YourNamespace {
    public class SqlHierarchyIdUserType : IUserType {
    public bool Equals(object x, object y) {
        if(ReferenceEquals(x, y))
            return true;

        if(x == null || y == null)
            return false;

        return x.Equals(y);
    }

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

    public object NullSafeGet(IDataReader rs, string[] names, object owner) {
        object prop1 = NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if(prop1 == null)
            return null;

        return SqlHierarchyId.Parse(new SqlString(prop1.ToString()));
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index) {
        if(value == null) {
            ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
        } else {
            if(value is SqlHierarchyId) {
                SqlHierarchyId hId = (SqlHierarchyId)value;
                ((IDataParameter)cmd.Parameters[index]).Value = hId.ToString();
            }
        }
    }

    public object DeepCopy(object value) {
        if(value == null)
            return null;

        var sourceTarget = (SqlHierarchyId)value;
        SqlHierarchyId copy = SqlHierarchyId.Parse(sourceTarget.ToString());

        return copy;

    }

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

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

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

    public SqlType[] SqlTypes {
        get { return new[] { NHibernateUtil.String.SqlType }; }
    }

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

    public bool IsMutable {
        get { return true; }
    }
}
}

流式映射

Map(e => e.YourSqlHierarchyIdProperty)
    .Column("YourSqlHierarchyIdFieldName")
    .CustomType<SqlHierarchyIdUserType>();

阅读这篇文章:

将ActiveRecord映射到: C#

ActiveRecord使用[Property]属性来映射用户定义的类型.因此,对于您来说,它看起来像这样:

ActiveRecord uses a [Property] attribute to map User Defined Types. So for you it would look something like this:

public class YourDataObject {
    [Property(ColumnType="YourNamespace.SqlHierarchyIdUserType, YourNamespace")
    public virtual SqlHierarchyId YourSqlHierarchyIdProperty;
}

希望有帮助!

这篇关于NHibernate中的SQL 2008 HierarchyID支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆