本文介绍了在NHibernate中将空字符串映射为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
MyTable:
ID:string PrimaryKey
父:字符串引用MyTable - NOTNULL!
和Fluent NHibernate映射到
class MyTable
{
public virtual string ID {get; set;}
public virtual MyTable Parent {get;设置;}
}
我的问题是Parent应该在我的C#数据库中的Parent列是(空字符串),反之亦然。不幸的是,我不能改变列的类型来接受NULL!
我试图使用IEmptyInterceptor,但我不明白它的工作。
在此先感谢,
forki
解决方案
您需要有一个IUserType key列,它执行特殊的NULL值处理。
public MyTableMap()
{
Id x => x.EntryNo)
//由于PK是一个字符串,它必须由应用程序分配。
.GeneratedBy.Assigned()
.SetAttribute(type,typeof(SpecialNullValueStringType).AssemblyQualifiedName);
参考文献(x => x.Parent);
$ b $ public class SpecialNullValueStringType:IUserType
{
#region IUserType Members
public bool IsMutable
{
get {return假; }
}
类型返回类型
{
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;
}
返回值;
$ b $ 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;
公共对象DeepCopy(对象值)
{
返回值;
public object Replace(object original,object target,object owner)
{
return original;
公共对象汇编(对象缓存,对象所有者)
{
return cached;
公共对象反汇编(对象值)
{
返回值;
$ b $ public bool Equals(object x,object y)
{
if(ReferenceEquals(x,y))
{
返回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
}
I have a SQL Server DB with a recursive table:
MyTable:
ID : string PrimaryKey
Parent: string references MyTable - NOTNULL !!
and map with Fluent NHibernate to
class MyTable
{
public virtual string ID {get; set;}
public virtual MyTable Parent {get; set;}
}
My problem is that Parent should be null in my C# app if the column Parent is "" (empty string) in the database and vice versa. Unfortunately I can't change the column type to accept NULL!
I tried to use IEmptyInterceptor but I don't get it working.
Thanks in advance, forki
解决方案
You need to have an IUserType for the primary key column, which does the special NULL value handling.
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中将空字符串映射为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!