[英]NHibernate linq query with IUserType
In my project I use a IUserType (BooleanM1) that handles boolean values and writes -1 for true and 0 for false values to the database.在我的项目中,我使用了一个 IUserType (BooleanM1) 来处理 boolean 值并将 -1 表示为真,将 0 表示为假值到数据库。 So far everything works well.
到目前为止一切正常。 The mapping looks like this:
映射如下所示:
<property name="Active" column="ACTIVE" type="Core.Persistence.NH.Types.BooleanM1,Core.Test"/>
So if I do a query like the following因此,如果我执行如下查询
var pList = Session.Query<Test>().Where( c => c.Active ).ToList();
an exception is thrown:抛出异常:
NHibernate.QueryException: Unable to render boolean literal value [.Where[Core.Test.Domain.Test]
(NHibernate.Linq.NhQueryable`1[Core.Test.Domain.Test], Quote((c, ) => (c.Active)), )]
---> System.InvalidCastException: Das Objekt des Typs "NHibernate.Type.CustomType" kann nicht
in Typ "NHibernate.Type.BooleanType" umgewandelt werden.
The BooleanM1 implementation is like this: BooleanM1 的实现是这样的:
{
public class BooleanM1 : IUserType
{
public bool IsMutable
{
get { return false; }
}
public Type ReturnedType
{
get { return typeof(bool); }
}
public SqlType[] SqlTypes
{
get { return new[]{NHibernateUtil.Int16.SqlType}; }
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if(obj == null ) return false;
return ((string)obj == "-1" || (string)obj == "1") ? true : false;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if(value == null)
{
((IDataParameter) cmd.Parameters[index]).Value = DBNull.Value;
}
else
{
((IDataParameter) cmd.Parameters[index]).Value = (bool)value ? -1 : 0;
}
}
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(bool).GetHashCode() + 473 : x.GetHashCode();
}
}
Is this an known bug in the linq provider or is there something wrong with my UserType?这是 linq 提供程序中的已知错误,还是我的 UserType 有问题? Any help is appreciated.
任何帮助表示赞赏。
I had a similar issue with a UserType that does pretty much the same thing.我在 UserType 上遇到了类似的问题,它做的事情几乎相同。 I found that explicitely stating the equality in my queries resolved the problem.
我发现在我的查询中明确说明相等性解决了这个问题。
Try going from:尝试从:
var pList = Session.Query<Test>().Where( c => c.Active ).ToList();
to:至:
var pList = Session.Query<Test>().Where( c => c.Active == true ).ToList();
For some reason NHibernate's Linq provider is then able to figure it out.出于某种原因,NHibernate 的 Linq 提供程序随后能够弄清楚。
Have you solved this yet?你解决了吗?
Not sure if it is the same issue but I had something similar where the database field was nullable and the custom type specifies that you are returning a "bool" -> It may need to be changed to "bool? for the return type"不确定这是否是同一个问题,但我有类似的情况,数据库字段可以为空并且自定义类型指定您返回“bool”->它可能需要更改为“bool?返回类型”
public Type ReturnedType
{
get { return typeof(bool?); }
}
Fixed in version 4.1.0.已在 4.1.0 版中修复。 The user type should implement IEnhancedUserType to work correctly with the fix.
用户类型应实现 IEnhancedUserType 以正确使用该修复程序。 https://nhibernate.jira.com/browse/NH-2839
https://nhibernate.jira.com/browse/NH-2839
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.