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

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

问题描述

搜索了各种 NHibernate 列表,并没有给出明确的答案.SQL2008 方言没有't 似乎支持 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>();

阅读这篇文章:

Castle ActiveRecord:映射到 IUserType 和 Class inC#

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天全站免登陆