ActiveRecord 3 RC 1 与 NHibernate 3.2 导致意外异常

发布于 2024-12-01 07:21:32 字数 2003 浏览 3 评论 0原文

由于前几天我感到很冒险,所以我决定使用 NHibernate 3.2 编译 ActiveRecord 3 RC 1,看看会发生什么。
除了我修复的重大更改之外,我还遇到了有关 SessionScopes 和 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

看来该错误来自这里:

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>();
            }
        }
    }

What haschanged that now I have to打开一个新的SessionScope

Since I felt adventurous the other day I decided compiling ActiveRecord 3 RC 1 with NHibernate 3.2 and see what happens.
Besides the breaking changes which I fixed I encountered a very strange behavior regarding SessionScopes and Linq queries.
Usually I don't have to use a session scope when using a Linq query but after I compiled ActiveRecord 3 RC 1 with NHibernate 3.2 I got the following error:

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

It seems that the error comes from here:

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>();
            }
        }
    }

What has changed that now I have to open a new SessionScope?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌上青苔 2024-12-08 07:21:32

我在使用 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>();
        }
    }

I had some trouble too with the Queryable function. Although I did not have the sessions scope problem, I had trouble saving update to objects retrieved by IQueryable. It seems that the new session was never registered with the active scope. I also changed the scope retrieval so maybe this also helps for you:

   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>();
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文