繁体   English   中英

.Net Core控制台应用程序-EF Core SaveChangesAsync()不提交对数据库的更新

[英].Net Core Console App - EF Core SaveChangesAsync() not committing updates to database

为了测试更广泛项目的数据访问层,我编写了一个简单的控制台应用程序作为调用DAL的测试工具。

数据的保存分两步进行。 第一步是从报告中保存非常基本的信息,然后HAS HAS才能成功。第二步是将原始数据解密到模型中,然后将其保存,但是如果失败,这将不是一个大问题,并且将报告并稍后处理。

整个过程是异步运行的(它是REST的一部分),但是await _context.SaveChangesAsync()调用似乎没有等待并且应用程序退出并且没有数据被保存。

以下是数据库更新的简化示例

public async Task<bool> SubmitFlightReportAsync(string ReportData)
{
    try
    {
        // Save the primary data - MUST Succeed
        _logger.LogInformation("Saving primary data database ");
        Models.MyReport myRpt = new Models.MyReport()
        {
            Status = 1,
            RawData = ReportData
        };
        _context.MyReports.Add(myRpt);

        if (await _context.SaveChangesAsync() > 0)
        {
            _logger.LogInformation("Primary save complete");

            // Now update with all the extracted data - Optional Succeed
            try
            {
                _logger.LogInformation("Extracting secondary data");
                /*
                 * 
                 * Here is where we extract all the information from the Report Data 
                 * and update the Model, before saving
                 * 
                 */
                _logger.LogInformation("Saving extracted data");

                if (await _context.SaveChangesAsync() > 0)
                {
                    _logger.LogDebug("Secondary save complete");
                }

            }
            catch (Exception ex)
            {
                _logger.LogError("Error saving secondary data ");
            }
        }
        else
        {
            _logger.LogInformation("Primary save failed");
        }
    }
    catch (Exception ex)
    {
        _logger.LogCritcal("Something went horrobly wrong");
    }
}

.and从控制台应用程序中这样调用...

_logger.LogInformation("Starting application");
_fltSvc.SubmitFlightReportAsync("this is the report data as a string")
_logger.LogInformation("All done");

当我运行应用程序时,这是报告的日志记录...

object:Information: Starting application
object:Information: Saving primary data database

... Where's all the other logging messages?

object:Information: All done

如您所见,它到达第一个SaveChangesAsync的地步,稍作等待,好像在做某件事,然后跳回主线程.....,并且没有数据存储在数据库中。

我敢肯定这很简单,但是我可以看到...

谢谢。

在您的主系统中调用SubmitFlightReportAsync ,无需等待,如下所示。

public static void Main(string[] args)
{
    Task t = SubmitFlightReportAsync("bla");
    t.Wait(); //This is the line you need to have when working with console apps.
}

在控制台应用程序中使用Asyny和Await时有所不同。

还是看看以下内容: 在C#中使控制台应用程序异步?

调用SubmitFlightReportAsync ,您将启动操作的执行,然后返回到调用方。 当您立即结束应用程序时,该任务将没有机会执行。 您必须等待任务完成,然后才能假定一切已完成:

_logger.LogInformation("Starting application");
Task submitTask =
    Task.Run(() => _fltSvc.SubmitFlightReportAsync("this is the report data as a string"));
submitTask.Wait();
_logger.LogInformation("All done");

如果您从Main方法运行此代码并且使用的是C#7.0或更高版本,则还可以简单地创建一个async Main并在其中await

static async void Main() {
    //...
    _logger.LogInformation("Starting application");
    await _fltSvc.SubmitFlightReportAsync("this is the report data as a string")
    _logger.LogInformation("All done");
    //...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM