Linq 调用存储过程快速返回

Linq call to stored procedure returning to quickly

我正在尝试调用此方法:

private async Task<int> RemoveAllCurrentEntries()
{
  using (var ent = new WebPortalEntities())
  {
    // remove all existing price file entries based on a corporationid
    var NewOrderIdOut = new SqlParameter(Settings.Default.CorporationId.ToString(),
      System.Data.SqlDbType.UniqueIdentifier);
    NewOrderIdOut.Direction = System.Data.ParameterDirection.Output;

    return await ent.Database.ExecuteSqlCommandAsync(
      "exec [dbo].[DeleteAllCorpCustomersFromCustomerPrice] @NewOrderIdOut output",
      NewOrderIdOut);
  }
}

来自这个方法:

private async Task ExecutePriceFile()
{
  try
  {
    var pricefile = Path.Combine(
      Settings.Default.PriceFileDirectory,
      Settings.Default.PriceFileName);

    // verify file exists other no point in running the rest of the code
    if (!File.Exists(pricefile)) return;

    var parsedfile = ParsePriceFile(pricefile);

    if (!parsedfile.Any()) return;

    using (var ent = new WebnPortalEntities())
    {
      var deleteEntries = RemoveAllCurrentEntries();

      if (deleteEntries == Task.CompletedTask)
      {
        foreach (var priceFile in parsedfile)
        {
          var pf = new CustomerPrice();
          pf.CorporationId = Settings.Default.CorporationId;
          pf.CustomerPriceId = Guid.NewGuid();

          pf.AccountNumber = priceFile.AccountNumber;
          pf.BranchNumber = priceFile.BranchNumber;
          pf.BudgetBalance = priceFile.BudgetBalance;
          pf.BudgetRate = priceFile.BudgetRate;
          pf.ContractDepositQuantity = priceFile.ContractDepositQuantity;
          pf.ContractGasPercent = priceFile.ContractGasPercent;
          pf.ContractGasQuantity = priceFile.ContractGasQuantity;
          pf.ContractGasTenths = priceFile.ContractGasTenths;
          pf.CreditLimit = priceFile.CreditLimit;
          pf.CurrentDate = priceFile.CurrentDate;
          pf.CurrentDiscountDays = priceFile.CurrentDiscountDays;
          pf.CurrentDiscountRate = priceFile.CurrentDiscountRate;
          //pf.CurrentDiscountRate2 = columns[22].ToDecimal();
          pf.CurrentDiscountRateDays = priceFile.CurrentDiscountRateDays;
          pf.CurrentDiscountRateType = priceFile.CurrentDiscountType;
          pf.CurrentDiscountType = priceFile.CurrentDiscountType;
          // pf.CurrentPrice2 = columns[19];
          pf.CurrentPrice = priceFile.CurrentPrice;
          pf.LastPaymentDate = priceFile.LastPaymentDate;
          pf.NonBudgetBalance = priceFile.NonBudgetBalance;
          pf.OldestDiscountDays = priceFile.OldestDiscountDays;
          pf.OldestDiscountRate = priceFile.OldestDiscountRate;
          pf.OldestDiscountType = priceFile.OldestDiscountType;
          pf.OldestPrice = priceFile.OldestPrice;
          pf.PastDueBalance = priceFile.PastDueBalance;
          pf.PaymentAmount = priceFile.PaymentAmount;
          pf.PreviousDate = priceFile.PreviousDate;
          pf.PreviousDiscountDays = priceFile.PreviousDiscountDays;
          pf.PreviousDiscountRate = priceFile.PreviousDiscountRate;
          pf.PreviousDiscountType = priceFile.PreviousDiscountType;
          pf.PreviousPrice = priceFile.PreviousPrice;
          pf.ProductCode = priceFile.ProductCode;
          pf.IsActive = priceFile.Status.ToActive();
          pf.UnitOfIssue = priceFile.UnitOfIssue;
          ent.CustomerPrices.Add(pf);

        }

        ent.SaveChanges();
      }
      else
      {
        throw new Exception(deleteEntries.Result.ToString());
      }
    }
  }
  catch (Exception exception)
  {
    Console.WriteLine(exception);
    throw;
  }
}

我没有让代码等到任务完成,它立即 returns 并发出此消息:

Id = 11, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed}"

我希望它等到完成后再 return。知道我做错了什么吗?

await RemoveAllCurrentEntries();

或者(如果你的主函数不是异步的)

Var _task = RemoveAllCurrentEntries();
_task.Wait();
Var result = _task.Result;