NHibernate 3.2的ActiveRecord 3 RC 1导致意外异常
因为前几天我觉得很冒险,所以我决定用NHibernate 3.2编译ActiveRecord 3 RC 1,看看会发生什么。NHibernate 3.2的ActiveRecord 3 RC 1导致意外异常,nhibernate,castle-activerecord,linq-to-nhibernate,Nhibernate,Castle Activerecord,Linq To Nhibernate,因为前几天我觉得很冒险,所以我决定用NHibernate 3.2编译ActiveRecord 3 RC 1,看看会发生什么。 除了我修复的破坏性更改之外,我还遇到了一个关于会话范围和Linq查询的非常奇怪的行为。 通常,在使用Linq查询时,我不必使用会话作用域,但在使用NHibernate 3.2编译ActiveRecord 3 RC 1后,我出现以下错误: Could not found a registered Scope. Linq queries needs a underlying
除了我修复的破坏性更改之外,我还遇到了一个关于会话范围和Linq查询的非常奇怪的行为。
通常,在使用Linq查询时,我不必使用会话作用域,但在使用NHibernate 3.2编译ActiveRecord 3 RC 1后,我出现以下错误:
Could not found a registered Scope. Linq queries needs a underlying a scope to be functional.
Stack Trace: at Castle.ActiveRecord.Framework.ActiveRecordLinqBase`1.get_Queryable()
at Castle.ActiveRecord.Framework.ActiveRecordLinq.AsQueryable[T]()
at Danel.Nursing.Scheduling.Actions.DataServices.BranchDataService.GetBranches() in D:\Work\Default\Scheduling\Danel.Nursing.Scheduling.Actions\DataServices\BranchDataService.cs:line 21
at Danel.Nursing.Scheduling.Controllers.SmallHoursAmountController.<>c__DisplayClassb.<SetBranches>b__a() in D:\Work\Default\Scheduling\Danel.Nursing.Scheduling\Controllers\SmallHoursAmountController.cs:line 275
at Danel.Nursing.Scheduling.Viewlets.WaitForAction.Worker_DoWork(Object sender
DoWorkEventArgs e) in D:\Work\Default\Scheduling\Danel.Nursing.Scheduling\Viewlets\WaitForAction.cs:line 40
找不到已注册的作用域。Linq查询需要一个底层作用域才能正常工作。
堆栈跟踪:位于Castle.ActiveRecord.Framework.ActiveRecordLinqBase`1.get_Queryable()
在Castle.ActiveRecord.Framework.ActiveRecordLinq.AsQueryable[T]()
位于D:\Work\Default\Scheduling\Danel.Warning.Scheduling.Actions\DataServices\BranchDataService.GetBranchs()中的Danel.Warning.Scheduling.Actions\DataServices.BranchDataService.cs:第21行
在D:\Work\Default\Scheduling\Danel.Warning.Scheduling\Controllers\SmallHoursAmountController.c\uu中显示ClassB.b\uu a(),位于D:\Work\Default\Scheduling\Danel.Warning.Scheduling\Controllers\SmallHoursAmountController.cs:第275行
在Danel.Nursing.Scheduling.Viewlets.WaitForAction.Worker\u DoWork(对象发送器
D:\Work\Default\Scheduling\Danel.pathing.Scheduling\Viewlets\WaitForAction.cs中的DoWorkEventArgs e):第40行
错误似乎来自这里:
public class ActiveRecordLinqBase<T> : ActiveRecordBase<T>
{
public static IOrderedQueryable<T> Queryable
{
get
{
var activeScope = holder.ThreadScopeInfo.GetRegisteredScope(); // The registered scope is null...
if (activeScope == null)
throw new ActiveRecordException("Could not found a registered Scope. Linq queries needs a underlying a scope to be functional.");
var key = holder.GetSessionFactory(typeof(T));
var session = activeScope.IsKeyKnown(key) ? activeScope.GetSession(key) : SessionFactoryHolder.OpenSessionWithScope(activeScope, key);
return session.AsQueryable<T>();
}
}
}
公共类ActiveRecordLinqBase:ActiveRecordBase
{
公共静态IOrderedQueryable可查询
{
得到
{
var activeScope=holder.ThreadScopeInfo.GetRegisteredScope();//注册的作用域为空。。。
如果(activeScope==null)
抛出新的ActiveRecordException(“找不到注册的作用域。Linq查询需要一个基础作用域才能正常工作。”);
var key=holder.GetSessionFactory(typeof(T));
var session=activeScope.IsKeyKnown(key)?activeScope.GetSession(key):SessionFactoryHolder.OpenSessionWithScope(activeScope,key);
返回会话。AsQueryable();
}
}
}
什么改变了我现在必须打开一个新的会话范围?我在使用Queryable函数时也遇到了一些问题。虽然我没有会话范围问题,但在保存对IQueryable检索到的对象的更新时遇到了问题。新会话似乎从未在活动范围中注册。我还更改了范围检索,因此这可能对您也有帮助:
public new IOrderedQueryable<T> Queryable
{
get
{
ISessionFactory key = ActiveRecordMediator<T>.GetSessionFactoryHolder().GetSessionFactory(typeof(T));
ISessionScope activeScope = SessionScope.Current;
if (activeScope == null)
throw new ActiveRecordException("Could not found a registered Scope. Linq queries needs a underlying a scope to be functional.");
var session = activeScope.IsKeyKnown(key) ? activeScope.GetSession(key) : OpenSessionWithScope(activeScope, key);
if (!activeScope.IsKeyKnown(key))
{
activeScope.RegisterSession(key,session);
}
return session.AsQueryable<T>();
}
}
公共新IOrderedQueryable可查询
{
得到
{
ISessionFactory key=ActiveRecordMediator.GetSessionFactoryHolder().GetSessionFactory(typeof(T));
ISessionScope activeScope=SessionScope.Current;
如果(activeScope==null)
抛出新的ActiveRecordException(“找不到注册的作用域。Linq查询需要一个基础作用域才能正常工作。”);
var session=activeScope.IsKeyKnown(key)?activeScope.GetSession(key):OpenSessionWithScope(activeScope,key);
如果(!activeScope.IsKeyKnown(key))
{
RegisterSession(键,会话);
}
返回会话。AsQueryable();
}
}
同样的AR和NH 3.1没有发生这种情况吗?@Mauricio Scheffer没有。@Mauricio Scheffer:那会有什么问题?