SaveChangesAsync方法不会保存更改(环境问题)

时间:2018-10-03 06:25:53

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

我的环境有问题。
问题和其他几个问题看起来一样:
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方法中获得“ point 2”,但从没有获得“ point 3”。同时没有抛出异常...看起来只是冻结的线程或类似的东西。

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

  

env:2012 R2 Standart,IIS 6.2,MSSQL数据库。

1 个答案:

答案 0 :(得分:0)

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

  

-在2018年10月3日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);
        }
    }
}