【问题标题】:SaveChangesAsync method doesn't save changes (environment troubles)SaveChangesAsync 方法不保存更改(环境问题)
【发布时间】:2018-10-03 06:25:52
【问题描述】:

我的环境有问题。
该问题与其他几个问题看起来相同:
SaveChangesAsync() method not updating database
Entity Framework 6.1.0 SaveChangesAsync

但是还有其他问题。就我而言,这段代码工作了几年,我确信问题与数据库有关,而不是这段代码,所以请帮助我了解我的环境。

也许我应该检查一下我的 db-user\db 吗?
此外,我还有其他一些服务器,它们的代码完全相同,运行良好。

我的代码:

public static async Task SaveResultsAsync(HbTestResult result)
{
    using (var data = new HbContex())
    {
        data.HbMonitor.Add(result);
        Logger.Instance.Error($" point 1 ");
        try
        {
            Logger.Instance.Error($"point 2");
            await data.SaveChangesAsync();
            Logger.Instance.Error($"point 3");
        }
        catch (Exception exception)
        {
            Logger.Instance.Error($"Ex_Db1.3: {exception.Message} and inner - chain: { ExceptionUtil.GetInnerExceptionsChainAsString(exception)}.");
        }
    }
}

我用另一种方法调用它:

private async Task<bool> PerformTestingAndGetAllResults(HbTest test, ConfigModel notificationConfig, Constants.TestStatus status)
{
    //**** (some variables declaring etc)
    var i = 0;
    while (i < numberOfTests)
    {
        await PerformTesting();
        if (!test.TestStatus.IsValid && test.TestStatus.Status == stat)
        {
            numberOfFailedTests++;
        }
        await DbUtil.SaveResultsAsync(SetTestResultsForDb());
        i++;
    }
    return numberOfFailedTests > notificationConfig.FailuresAllowed;
}

好吧,我也没有在这台机器上调试,所以我必须使用日志记录方法(但如果可以提供帮助,我可以完全访问数据库。

我的代码总是在 SaveResultsAsync 方法中获得“第 2 点”,但从未获得“第 3 点”。同时没有抛出异常......看起来线程刚刚冻结或类似。

使用来自connectionString 的凭据,我可以手动对我的数据库执行任何操作(使用ssms)。

环境:2012 R2 标准,IIS 6.2,MSSQL 数据库。

【问题讨论】:

  • 链接的问题不指向特定问题。如果您检查链接的问题,您会发现它们都是开发人员错误。在一种情况下,使用了错误的包。另一种情况是,代码本身陷入了僵局。
  • 使用 Activity Monitor 或 SQL Server(顺便问一下是哪个版本?)Profiler 或扩展事件来查看服务器上当前运行的内容以及语句是否已执行。可能是 SQL 命令被阻塞并等待其他命令,或者它们花费的时间太长。为每条记录调用SaveChangesAsync 会导致巨大的延迟,因为必须为每条记录执行一个新查询。为什么不直接将所有记录添加到上下文中并调用一次 SaveChangesAsync?
  • @PanagiotisKanavos 13.0.4001.0 64 位。行。谢谢,看看如何使用分析器,让你知道
  • 您可以在 EF 6.1 中启用日志记录。 by adding a DatabaseLogger interceptor 在 web.config 中。这可以显示有关客户端实际发生的事情的更多详细信息

标签: c# sql-server asp.net-mvc entity-framework


【解决方案1】:

感谢@PanagiotisKanavos。我已经实现了 EF6 拦截器并遇到了问题。问题是:错误 1827,创建数据库\更改数据库操作的许可证限制 (10240 MB)。我不知道为什么实体不将其作为常见异常抛出,但它出现在如下日志中:

-- 在 10/3/2018 2:50:42 PM +05:30 异步执行 -- 在 20 毫秒内失败并出现错误:无法为数据库中的对象 'dbo.table'.'PK_table' 分配空间 'dbname' 因为 'PRIMARY' 文件组已满。创造 通过删除不需要的文件、将对象放入 文件组,将其他文件添加到文件组,或设置 为文件组中的现有文件启用自动增长。

所以,有用的是链接:https://docs.microsoft.com/en-us/ef/ef6/fundamentals/configuring/config-file#logging-database-operations-to-a-file-ef61-onwardshttp://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx

所以,最终代码是: 1) web.config

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
    <interceptors>
      <interceptor type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework">
        <parameters>
          <parameter value="C:\inetpub\wwwroot\App_Data\LogOutput.txt"/>
          <parameter value="true" type="System.Boolean"/>
        </parameters>
      </interceptor>
    </interceptors>
  </entityFramework>

2) 和这个类在任何地方:

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity.Infrastructure.Interception;
using System.Linq;
using System.Web;

namespace MobileConnectHM.Utils
{
    public class EFCommandInterceptor : IDbCommandInterceptor
    {
        public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
            LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
            LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
        }

        private void LogInfo(string command, string commandText)
        {
            Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多