【问题标题】:int does not contain a definition for getAwaiter errorint 不包含 getAwaiter 错误的定义
【发布时间】:2020-02-17 10:21:29
【问题描述】:

我正在 asp.net 核心中实现 CRUD。我创建了如下方法:

  public async Task<int> AccessAPIApplicantHistoryDouble(int t)
    {
        var task = APIApplicantHistoryLog(t);
        int result = await task;
        return result;

    }

  private int APIApplicantHistoryLog(int myAPIApplicantId)
    {
        APIApplicantHistoryDto mydto = new APIApplicantHistoryDto();

          _context.Set<ApiApplicantHistory>().Add(new ApiApplicantHistory
            {
                ApiApplicantId = myAPIApplicantId,
                Date = null,
                SentResponseType = 0,
                UnconfirmedReason = 0,
                LastReqStatus = 0,
                Description = "",
                IdDeleted = false   // This field should be totally omited from SQL server
            }) ;
             _context.SaveChangesAsync();


            var myArrayList = new List<int>();

            var result = from x in _context.ApiApplicantHistory where x.ApiApplicantId == myAPIApplicantId
                         select new { x.Id };

            foreach (var i in result)
                myArrayList.Add(i.Id);

            return myArrayList.Max(); 
         }

在 await 行中出现如下错误:

如果有人建议我解决该错误的解决方案,我将不胜感激。

【问题讨论】:

  • APIApplicantHistoryLog 不是“异步”类,因此您不能“等待”它。 “await” 期望一个类返回一个 Task 对象。 APIApplicantHistoryLog 返回 int。使其异步并返回 Task 。另外,有“await _context.SaveChangesAsync();”

标签: asp.net-core-mvc


【解决方案1】:

当你调用像SaveChangesAsync这样的异步方法时,你的代码应该await得到结果:

await _context.SaveChangesAsync();

一旦你的代码使用await那里,C# 编译器会给你一个错误,告诉你标记APIApplicantHistoryLog 方法async 并从int 更改它的返回类型到Task&lt;int&gt;。一旦你这样做了,那么你其他方法中的await 将正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2012-08-04
    • 2017-04-20
    • 2017-11-20
    • 2018-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多