解决  更新问题

Raysefo

知名会员
已加入
2019年2月22日
留言内容
194
编程经验
10+
你好,

我正在尝试更新表格。但事实并非如此。怎么会,我不明白。也没有错误。

C#:
...
var gameBankResult =
                    await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                        g.productCode == requestDto.productCode && g.referenceId == Guid.Empty); --> returns 1 result

                //If we have exact number of games in our database, mark them!
                if (gameBankResult.Count() != 0 && gameBankResult.Count() >= requestDto.quantity)
                {
                    for (var index = 0; index < requestDto.quantity; index++)
                    {
                        var item = gameBankResult[index];
                        item.referenceId = gameRequest.referenceId;
                        item.requestDateTime = DateTime.Now;
                        item.responseDateTime = DateTime.Now;
                        _unitOfWork.GameBankRepository.Update(item);
                        await _unitOfWork.SaveAsync(); ---> does not update the table
                    }

                    //Query GameBank database
                    var gameBankConfirmResult =
                        await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                            g.referenceId == gameRequest.referenceId); ---> returns no result
                ...

通用回购:
C#:
public virtual void Update(TEntity entityToUpdate)
        {
            dbSet.Attach(entityToUpdate);
            context.Entry(entityToUpdate).State = EntityState.Modified;
        }

工作单位:
C#:
public async Task SaveAsync()
        {
            await _context.SaveChangesAsync();
            
        }
 

跳伞

工作人员
已加入
2019年4月6日
留言内容
2,536
地点
弗吉尼亚州切萨皮克
编程经验
10+
如果在UnitOfWork.SaveAsync()上设置一个断点,它甚至会被调用吗?
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,933
地点
英国
编程经验
10+
如果您需要了解调试器以及如何设置断点的帮助,可以在我的签名中找到相应的链接。
 

Raysefo

知名会员
已加入
2019年2月22日
留言内容
194
编程经验
10+
如果在UnitOfWork.SaveAsync()上设置一个断点,它甚至会被调用吗?

是的,我发现这是由于交易范围所致。如果我不使用事务作用域,它将立即更新,但是这一次如果插入/更新之一失败,我该如何回滚。

C#:
private async Task<HttpResponseMessage> CallGame(RequestDto requestDto)
        {
            
            HttpResponseMessage response = null;
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                //Transform DTO into GameRequest for calling Game Initiate
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<RequestDto, GameRequest>();
                    cfg.CreateMap<GameRequest, GameConfirmRequest>();
                    cfg.CreateMap<GameBank, GameConfirmResponse>();
                    cfg.CreateMap<GameBankPin, Coupon>();
                    cfg.CreateMap<GameRequest, GameRequestDto>();
                });
                var iMapper = config.CreateMapper();
                var gameRequest = iMapper.Map<RequestDto, GameRequest>(requestDto);
                //Unique reference ID
                gameRequest.referenceId = Guid.NewGuid();

                var gameRequestDto = iMapper.Map<GameRequest, GameRequestDto>(gameRequest);
                //Create signature
                gameRequest = Utilities.CreateSignature(gameRequestDto, RequestType.Initiate);

                //Add initiation request into database
                _unitOfWork.GameRepository.Insert(gameRequest);

                //Query GameBank database
                var gameBankResult =
                    await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                        g.productCode == requestDto.productCode && g.referenceId == Guid.Empty);

                //If we have exact number of games in our database, mark them!
                if (gameBankResult.Count() != 0 && gameBankResult.Count() >= requestDto.quantity)
                {
                    for (var index = 0; index < requestDto.quantity; index++)
                    {
                        var item = gameBankResult[index];
                        item.referenceId = gameRequest.referenceId;
                        item.requestDateTime = DateTime.Now;
                        item.responseDateTime = DateTime.Now;
                        _unitOfWork.GameBankRepository.Update(item);
                        await _unitOfWork.SaveAsync();
                    }

                    //Query GameBank database
                    var gameBankConfirmResult =
                        await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                            g.referenceId == gameRequest.referenceId);
                    if (gameBankConfirmResult != null)
                    {
                        if (gameBankConfirmResult.Count == 1)
                        {
                            var gameBankConfirmResponse = iMapper.Map<IList<GameBank>, IList<GameConfirmResponse>>(gameBankConfirmResult);
                            gameBankConfirmResponse[0].purchaseStatusDate = DateTime.Now;
                            //Add confirm response into database
                            _unitOfWork.GameConfirmResponseRepository.Insert(gameBankConfirmResponse[0]);
                            var resultResponse = JsonConvert.SerializeObject(
                                gameBankConfirmResponse[0],Formatting.Indented,
                            new JsonSerializerSettings()
                            {
                                ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                            });
                            response = new HttpResponseMessage
                            {
                                StatusCode = System.Net.HttpStatusCode.OK,
                                Content = new StringContent(
                                    resultResponse, System.Text.Encoding.UTF8,
                                    "application/json"),
                            };
                        }
                        else if(gameBankConfirmResult.Count > 1)
                        {
                            var gameResult = new GameConfirmResponse
                            {
                                coupons = new List<Coupon>()
                            };
                            var price = 0.0;
                            var quantity = 0;

                            foreach (var item in gameBankConfirmResult)
                            {
                                price = price + item.unitPrice;
                                quantity = quantity + 1;
                                foreach (var coupons in item.coupons)
                                {
                                    var gameCouponResult = new Coupon()
                                    {
                                        expiryDate = coupons.expiryDate,
                                        Pin = coupons.Pin,
                                        Serial = coupons.Serial
                                    };
                                    //Add coupon values
                                    gameResult.coupons.Add(gameCouponResult);
                                }

                            }

                            //Set summed/counted values
                            gameResult.referenceId = gameBankConfirmResult[0].referenceId;
                            gameResult.productCode = gameBankConfirmResult[0].productCode;
                            gameResult.quantity = quantity;
                            gameResult.currency = gameBankConfirmResult[0].currency;
                            gameResult.unitPrice = gameBankConfirmResult[0].unitPrice;
                            gameResult.totalPrice = price;
                            gameResult.productDescription = gameBankConfirmResult[0].productDescription;
                            gameResult.totalPayablePrice = price;

                            //var gameBankConfirmResponse = iMapper.Map<GameBank, GameConfirmResponse>(gameResult);
                            //Add confirm response into database
                            _unitOfWork.GameConfirmResponseRepository.Insert(gameResult);
                            var resultResponse = JsonConvert.SerializeObject(
                                gameResult, Formatting.Indented,
                                new JsonSerializerSettings()
                                {
                                    ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
                                });
                            response = new HttpResponseMessage
                            {
                                StatusCode = System.Net.HttpStatusCode.OK,
                                Content = new StringContent(
                                    resultResponse, System.Text.Encoding.UTF8,
                                    "application/json"),
                            };
                        }
                    }
                }

                await _unitOfWork.SaveAsync();

                scope.Complete();
            }

            return response;
        }
 

金西尼

C#论坛主持人
工作人员
已加入
2011年4月23日
留言内容
3,525
地点
悉尼,澳大利亚
编程经验
10+
你不能吃蛋糕也不能吃。如果需要能够回滚,那么就需要一个事务,并且在事务完成之前,不会提交该事务中发生的任何事情。实际问题是什么?无论如何,您之后立即进行交易吗?
 

Raysefo

知名会员
已加入
2019年2月22日
留言内容
194
编程经验
10+
我想我需要改变逻辑。长话短说,我正在查询基于具有空GUID的产品代码的表。

C#:
/Query GameBank database
                var gameBankResult =
                    await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                        g.productCode == requestDto.productCode && g.referenceId == Guid.Empty);

如果表中有合适的记录,我将对其进行更新。我也在这里设置手动生成的referenceId。

C#:
if (gameBankResult.Count() != 0 && gameBankResult.Count() >= requestDto.quantity)
                {
                    for (var index = 0; index < requestDto.quantity; index++)
                    {
                        var item = gameBankResult[index];
                        item.referenceId = gameRequest.referenceId;
                        item.requestDateTime = DateTime.Now;
                        item.responseDateTime = DateTime.Now;
                        _unitOfWork.GameBankRepository.Update(item);
                        await _unitOfWork.SaveAsync();
                    }

然后,我再次在此更新的表中查询该特定的referenceId并生成响应消息。

C#:
 var gameBankConfirmResult =
                        await _unitOfWork.GameBankRepository.GetGamesAsync(g =>
                            g.referenceId == gameRequest.referenceId);
                    if (gameBankConfirmResult != null)
                    {
                        if (gameBankConfirmResult.Count == 1)

我认为第二个查询不是必需的。我应该尝试从第一个查询生成响应。
 
最佳 底部