如何避免数据库产生价值

Raysefo

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


为什么数据库没有实现就产生价值?我跟踪了方法,GUID最初是生成的,但从未更改过。在SaveAsync之后,数据库会以不同的referenceId生成相同的值。

C#:
private async Task<HttpResponseMessage> CallRazerService(RequestDto requestDto)
        {
            HttpResponseMessage response = null;

            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                //Transform DTO into GameRequest for calling Razer Initiate
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap<RequestDto, GameRequest>();
                    cfg.CreateMap<GameRequest, GameConfirmRequest>();
                    cfg.CreateMap<GameConfirmResponse, GameConfirmResponseDto>();
                    cfg.CreateMap<Coupon, CouponDto>();
                    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);

                #region Call Razer initiate/confirm

                //Call Razer for initiation
                response = await Utilities.CallRazer(gameRequest, "purchaseinitiation");

                //Read response
                var htmlResponse = await response.Content.ReadAsStringAsync();
                var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);

                //Adding initiation response into database
                _unitOfWork.GameResponseRepository.Insert(gameResponse);

                if (gameResponse.initiationResultCode == "00")
                {
                    gameRequestDto = iMapper.Map<GameRequest, GameRequestDto>(gameRequest);
                    gameRequestDto.validatedToken = gameResponse.validatedToken;
                    //Create signature
                    var gameConfirmRequest = Utilities.CreateSignature(gameRequestDto, RequestType.Confirm);

                    //Transform DTO into GameRequest for calling Razer Initiate
                    var gameConfirmRequests = iMapper.Map<GameRequest, GameConfirmRequest>(gameConfirmRequest);

                    //Add confirm request into database
                    _unitOfWork.GameConfirmRequestRepository.Insert(gameConfirmRequests);

                    //Call Razer for confirm
                    response = await Utilities.CallRazer(gameConfirmRequest, "purchaseconfirmation");

                    //Read response
                    htmlResponse = await response.Content.ReadAsStringAsync();
                    var gameConfirmResponse = JsonConvert.DeserializeObject<GameConfirmResponse>(htmlResponse);

                    //Add confirm response into database
                    _unitOfWork.GameConfirmResponseRepository.Insert(gameConfirmResponse);
                }

                #endregion

                await _unitOfWork.SaveAsync();
                scope.Complete();
            }

            return response;
        }

C#:
public class Purchase
    {
        public int Id { get; set; }
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid referenceId { get; set; }

C#:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //Configure Null Column
            modelBuilder.Entity<GameRequest>()
                .Property(p => p.referenceId)
                .IsOptional();
            modelBuilder.Entity<GameRequest>().Property(t => t.referenceId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        }
 

羊皮

退休程序员
工作人员
已加入
2018年9月5日
留言内容
1,923
地点
英国
编程经验
10+
您实际上为相同的代码打开了主题。 搜索结果查询:购买确认

您没有收到其他主题的充分答案吗?

最后我记得,您已经更改了一个字段来为您生成ID。从那以后你有回滚吗?
 

Raysefo

知名会员
已加入
2019年2月22日
留言内容
192
编程经验
10+
是的,我回滚了它,但是现在它仍然以某种方式自动生成。我删除了表,迁移历史记录,但是仍然生成了这个referenceId数据库。
有两种不同的方法;首先,一个方法被调用。如果没有响应,则调用方法二。因此案件结案。
 
Last edited:
最佳 底部