【问题标题】:Entity Framework 5 command timeout error after changing target Framework version and re-installing EF更改目标框架版本并重新安装 EF 后实体框架 5 命令超时错误
【发布时间】:2012-10-13 18:32:39
【问题描述】:

这里是背景:我构建了一个新的 WCF 服务,它使用 EF5 并针对 .Net Framework 4.5。我对现有的大型遗留数据库使用了代码优先。在我将服务部署到测试服务器之前,一切都运行良好,当时我发现测试服务器(因此生产服务器,因为它们是相同的版本)无法支持 Framework 4.5。

因此,我将项目降级为针对 Framework 4.0。在重新编码我所有的实体映射后(以避免使用特定于 4.5 的 DataAnnotations.Schema 命名空间中的代码),我让这个在本地再次工作。但是,在部署到服务器后,我遇到了此处描述的错误:Can anyone spot why I keep getting this error testing the EF 5 beta

按照建议进行操作后 - 通过 NuGet 卸载并重新安装 EF5,我现在在尝试测试服务时遇到超时错误。

特别是在尝试执行以下 Linq 时发生超时:

var games = from m in _db.Members
        join s in _db.UkSyndicates
            on m.MemberId equals s.MemberId
        where m.PaymentMethod == "Credit/Debit Card"
        && EntityFunctions.TruncateTime(s.NextChargeDate) <=  EntityFunctions.TruncateTime(DateTime.Now)
        && !string.IsNullOrEmpty(m.AibCustomerReference)
        && (m.StatusId == 105 || m.StatusId == 113)
        select new BillingItem
        {
            Id = s.Id,
            SyndicateId = s.SyndicateId,
            MemberId = s.MemberId,
            LastDrawId = s.LastDrawId,
            LastPaidGame = s.LastPaidGame,
            NextChargeDate = s.NextChargeDate,
            Protected = s.Protected,
            PaymentFrequency = (int)(s.PaymentFrequency*4),
            CurrencyCode = m.CurrencyCode,
            AibCustomerReference = m.AibCustomerReference,
            WeeklyFee = (from f in _db.GameFees where f.FeeName == "UK_LOTTO_FEE" && f.CurrencyCode == m.CurrencyCode select f.Amount).FirstOrDefault(),
            GameCode = game,
            PostCode = m.PostCode,
            CountryCode = m.CountryCode,
            EmailAddress = m.Email
        };

很可能有一种更有效的方法来编写此查询 - 我对 Linq 和 EF 相当缺乏经验,但我要强调这在卸载和重新安装 EF 之前不会超时。事实上,它返回数据的速度相当快。

我看不出有什么原因会在重新安装 EF 后超时,代码中没有任何变化,但需要快速解决这个问题!

谢谢。

为了澄清我得到的错误,在 EF 尝试执行上述查询时,我得到了一个 EntityCommandExecutionException 抛出。异常消息是“执行命令定义时发生错误。有关详细信息,请参阅内部异常。”内部异常是“超时已过期。在操作完成之前超时时间已过或服务器没有响应。”

堆栈跟踪如下:

在 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior 行为) 在 System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext 上下文,ObjectParameterCollection 参数值) 在 System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) 在 System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() at System.Data.Entity.Internal.Linq.InternalQuery1.GetEnumerator() 在 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() at RepeatBillingService.Repositories.BillingRepository.GetMembersForGame(String game, List1& billingQ) 在 p:\Projects\BigFatLottos\RepeatBillingService\RepeatBillingService\Repositories\BillingRepository.cs:line 441 在 p:\Projects\BigFatLottos\RepeatBillingService\RepeatBillingService\Repositories\BillingRepository.cs:line 24 中的 RepeatBillingService.Repositories.BillingRepository.GetMemberGamesForBilling() 在 p:\Projects\BigFatLottos\RepeatBillingService\RepeatBillingService\RepeatBillingService.svc.cs:line 51 中的 RepeatBillingService.RepeatBillingService.RunRepeatBilling() 在 SyncInvokeRunRepeatBilling(对象,对象 [],对象 []) 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象 [] 输入,对象 [] 和输出) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

【问题讨论】:

  • 为什么对现有数据库优先使用代码?当然,自然的选择首先是数据库?这对我来说似乎很奇怪,虽然这不是我第一次看到这个决定......
  • 什么是超时?数据库还是服务?
  • @flem 感谢您的回复。数据库是一个巨大的遗留物,并且充满了名称不佳的列等。我只需要大约 15 个表来完成这项特定的工作,所以想编写“干净”的实体并将它们映射到所需的表。所以代码优先似乎是要走的路——但可能有更好的方法,正如我所说的,我对 EF 很陌生。
  • 我可以在调试中单步执行该服务,但在执行发布的 linq 时会超时。在过去的几分钟里,我使用了试错法,简化了查询,当大多数条件被删除时,它会成功(并且立即)返回数据。因此,似乎这是一个将条件一一放回以找出罪魁祸首的情况。但我不明白为什么在重新安装 EF 之前这很好。
  • 除此之外,问题似乎出在 EntityFunctions.TruncateTime 的使用上。这是一个已知的问题?该方法是否仅在 Framework 4.5 中支持?重新定位项目后,我仍然获得智能感知,并且项目构建并运行,所以不会指望它会导致问题。

标签: wcf entity-framework code-first


【解决方案1】:

我明白了这一点,并道歉,因为整个问题有点牵强附会。我在后台打开了一个 SSMS 会话,我正在调整数据以进行测试,并且一直在更新“NextChargeDate”值。当我要关闭会话时,我得到“有未提交的事务,你想现在提交吗”这开始敲响了警钟。

果然重启WCF服务,一切正常。所以我的超时是由于数据库中该字段上未提交的更新而发生的 - EF 显然在提交更新之前无法成功读取数据。

所以我很抱歉在这个问题上浪费了您的时间,这只是一个简单的疏忽,同时在压力下长时间工作以启动新系统。

感谢您的回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多