实体框架SaveChanges()不起作用

时间:2018-01-23 10:37:11

标签: c# entity-framework entity-framework-6 asp.net-web-api2

调用API进行更新时遇到问题,savechanges()无效(数据未更新)。

但是,当我正确添加Thread.Sleep(1000);数据更新时。

工作方法

public async Task<ResponseBaseModel> AddOrderRemark2(AddOrderRemarkRequestModel model)
{
    try
    {
        using (ChatEntities context = new ChatEntities(CurrentUsername))
        {
            List<string> statusList = getPendingStatus(context).Result;
            OrderHeader orderHeader = getOrderHerderByOrderCode(context, model.OrderCode, model.SalesChannelId).Result;

            if (statusList.Contains(orderHeader.Status))
            {
                if (orderHeader != null)
                {
                    Thread.Sleep(1000);
                    orderHeader.Remark = model.Remark;
                    context.DBEntry(orderHeader, EntityState.Modified);
                    context.SaveChanges();
                }

            }

        }
        return new ResponseBaseModel(MessageCode.OK);
    }
    catch (Exception ex)
    {
        return new ResponseBaseModel(MessageCode.Fail, ex.InnerException.Message);
    }
}

失败方法

public async Task<ResponseBaseModel> AddOrderRemark2(AddOrderRemarkRequestModel model)
{
    try
    {
        using (ChatEntities context = new ChatEntities(CurrentUsername))
        {
            List<string> statusList = getPendingStatus(context).Result;
            OrderHeader orderHeader = getOrderHerderByOrderCode(context, model.OrderCode, model.SalesChannelId).Result;

            if (statusList.Contains(orderHeader.Status))
            {
                if (orderHeader != null)
                {
                    orderHeader.Remark = model.Remark;
                    context.DBEntry(orderHeader, EntityState.Modified);
                    context.SaveChanges();
                }

            }

        }
        return new ResponseBaseModel(MessageCode.OK);
    }
    catch (Exception ex)
    {
        return new ResponseBaseModel(MessageCode.Fail, ex.InnerException.Message);
    }
}

修改

我意识到客户端同时有两个API调用。此外,这两个API在同一个表'OrderHeader'上更新,其中包含接收者信息备注,这就是导致此问题的原因!!我怎么能防止这个问题呢?

        [HttpPost]
        [ActionName("AddReceiverAddress")]
        [ChatAuthentication]
        public async Task<ResponseBaseModel> AddReceiverAddress(AddReceiverAddressRequestModel model)
        {
            return _orderService.Value.AddReceiverAddress(model).Result;
        }

        [HttpPost]
        [ActionName("AddOrderRemark")]
        [ChatAuthentication]
        public async Task<ResponseBaseModel> AddOrderRemark(AddOrderRemarkRequestModel model)
        {
            return _orderService.Value.AddOrderRemark(model).Result;
        }

2 个答案:

答案 0 :(得分:3)

您没有正确使用异步。试试这个

public async Task<ResponseBaseModel> AddOrderRemark2(AddOrderRemarkRequestModel model)
{
    try
    {
        using (ChatEntities context = new ChatEntities(CurrentUsername))
        {
            List<string> statusList = await getPendingStatus(context);
            OrderHeader orderHeader = await getOrderHerderByOrderCode(context, model.OrderCode, model.SalesChannelId);

答案 1 :(得分:0)

当您调用此方法时,您是await还是Wait()才能获得结果?

当您调用该方法时,您必须执行以下任一操作。

await AddOrderRemark2(model);

或者

AddOrderRemark2(model).Wait();