nhibernate - 在 NHibernate 中将空字符串映射到 NULL

标签 nhibernate fluent-nhibernate

我有一个带递归表的 SQL Server 数据库:

MyTable:
 ID : string PrimaryKey
 Parent: string references MyTable - NOTNULL !!

并用 Fluent NHibernate 映射到

class MyTable
{
  public virtual string ID {get; set;}
  public virtual MyTable Parent {get; set;}
}

我的问题是,如果列 Parent 在数据库中为“”(空字符串),则 Parent 在我的 C# 应用程序中应该为空,反之亦然。不幸的是,我无法更改列类型以接受 NULL!

我尝试使用 IEmptyInterceptor,但无法正常工作。

提前致谢, fork

最佳答案

您需要为主键列有一个 IUserType,它执行特殊的 NULL 值处理。

public MyTableMap()
{
    Id(x => x.EntryNo)
        // Since the PK is a string, it must be assigned by the application.
        .GeneratedBy.Assigned()
        .SetAttribute("type", typeof(SpecialNullValueStringType).AssemblyQualifiedName);

    References(x => x.Parent);
}

public class SpecialNullValueStringType : IUserType
{
    #region IUserType Members
    public bool IsMutable
    {
        get { return false; }
    }

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

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

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

        if (obj == null)
        {
            return null;
        }

        var value = (string) obj;
        if (String.IsNullOrEmpty(value))
        {
            return null;
        }

        return value;
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        if (value == null)
        {
            ((IDataParameter) cmd.Parameters[index]).Value = String.Empty;
        }
        else
        {
            ((IDataParameter) cmd.Parameters[index]).Value = value;
        }
    }

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

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

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

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

    public new 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 == null ? typeof(string).GetHashCode() + 473 : x.GetHashCode();
    }
    #endregion
}

关于nhibernate - 在 NHibernate 中将空字符串映射到 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1187841/

相关文章:

c# - 无法使用 Nhibernate 为 SQL Server CE 创建架构

performance - NHibernate + Fluent 启动时间长

c# - NHibernate 两列同时作为复合键和外键

休眠升级

nhibernate - 使用 LINQ to NHibernate 按日期分组

c# - 如何正确测试 NHibernate FetchMode.Eager?

database - 将 NHibernate 与查找表结合使用

c# - 如何使用 NHibernate 创建标签系统(多对多)

C# 字符串枚举作为 NHibernate 鉴别器

.net - NHibernate - 如何映射到没有表的类(用于自定义 sql 查询)