我一直在升级我的NHibernate安装(仍然是1.2!)。在我尝试设置拦截器之前一切都很好。如果我向OpenSession()调用添加一个Interceptor,那么在尝试从DB加载实体时会出现此错误:
“无效的转换(检查映射是否存在属性类型不匹配); System.Object的设置者”
如果没有拦截器,则不会发生此错误。
拦截器非常简单,事实上我几乎已经注释了所有内容以便进行测试:
public class Interceptor : EmptyInterceptor
{
public override void AfterTransactionBegin(ITransaction tx)
{
}
public override void AfterTransactionCompletion(ITransaction tx)
{
}
public override void BeforeTransactionCompletion(ITransaction tx)
{
}
//public override int[] FindDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
//{
//}
//public override object GetEntity(string entityName, object id)
//{
//}
//public override string GetEntityName(object entity)
//{
//}
public override object Instantiate(string entityName, EntityMode entityMode, object id)
{
//((EntityBase)entity).Instantiate(entityName, entityMode, id)
return new object();
}
//public override bool? IsTransient(object entity)
//{
//}
public override void OnCollectionRecreate(object collection, object key)
{
}
public override void OnCollectionRemove(object collection, object key)
{
}
public override void OnCollectionUpdate(object collection, object key)
{
}
public override void OnDelete(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
//((EntityBase)entity).OnDelete(entity, id, state, propertyNames, types);
}
//public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, IType[] types)
//{
//}
public override bool OnLoad(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
//if (entity.GetType().FullName.Equals("EntityBase"))
// return ((EntityBase)entity).OnLoad(entity, id, state, propertyNames, types);
//else
return false;
}
//public override NHibernate.SqlCommand.SqlString OnPrepareStatement(NHibernate.SqlCommand.SqlString sql)
//{
//}
public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, IType[] types)
{
//return ((EntityBase)entity).OnSave(entity, id, state, propertyNames, types);
return true;
}
public override void PostFlush(System.Collections.ICollection entities)
{
}
public override void PreFlush(System.Collections.ICollection entities)
{
}
public override void SetSession(ISession session)
{
}
}
因此我完全感到困惑。就像我说的那样,使用Interceptor它会在加载和保湿实体上出错(在OnLoad事件触发后),没有拦截器就可以了。我正在使用nHibernate 3.2.0.2001和.Net4来对抗SQL Server 2008。
任何帮助表示赞赏!本
不幸的是我一直无法解决这个问题,因为这个特定的问题我不得不切换到Linq-SQL,而且我在使用这些模板所需的时间范围内找不到解决方案。因此,我目前无法提供更多信息。