Cenk

Cenk

  • NA
  • 92
  • 13.2k

The changes to the database were committed successfully, but

Jul 20 2019 6:17 AM
Hi,

I have a asp.net web API. I am using EF 6 code first approach. I am getting this error only when I load testing my web API with Jmeter. How can I fix this?
 
  1. The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Saving or accepting changes failed because more than one entity of type 'BusinessEntity.GameResponse' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration 
 Here is my GameResponse:
  1. using System;  
  2.   
  3. namespace BusinessEntity  
  4. {  
  5.     public class GameResponse  
  6.     {  
  7.         public int Id { getset; }  
  8.         public string referenceId { getset; }  
  9.         public string productCode { getset; }  
  10.         public int quantity { getset; }  
  11.         public string version { getset; }  
  12.         public string signature { getset; }  
  13.         public string ApplicationCode { getset; }  
  14.         public string validatedToken { getset; }  
  15.         public DateTime? responseDateTime { getset; } = DateTime.Now;  
  16.         public string initiationResultCode { getset; }  
  17.         public string companyToken { getset; }  
  18.         public string estimateUnitPrice { getset; }  
  19.         public string currency { getset; }  
  20.     }  

 Here is the method I am load testing:
 
  1. private async Task<HttpResponseMessage> CallRazerService(RequestDto requestDto)  
  2.         {  
  3.             HttpResponseMessage response = null;  
  4.   
  5.             using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))  
  6.             {  
  7.                 try  
  8.                 {  
  9.                     //Transform DTO into GameRequest for calling Razer Initiate  
  10.                     var config = new MapperConfiguration(cfg =>  
  11.                     {  
  12.                         cfg.CreateMap<RequestDto, GameRequest>();  
  13.                         cfg.CreateMap<GameRequest, GameConfirmRequest>();  
  14.                         cfg.CreateMap<GameConfirmResponse, GameConfirmResponseDto>();  
  15.                         cfg.CreateMap<Coupon, CouponDto>();  
  16.                     });  
  17.                     var iMapper = config.CreateMapper();  
  18.                     var gameRequest = iMapper.Map<RequestDto, GameRequest>(requestDto);  
  19.                     //Unique reference ID  
  20.                     gameRequest.referenceId = Guid.NewGuid().ToString();  
  21.   
  22.                     //Create signature  
  23.                     gameRequest = Utilities.CreateSignature(gameRequest, RequestType.Initiate);  
  24.   
  25.                     //Add initiation request into database  
  26.                     _unitOfWork.GameRepository.Insert(gameRequest);  
  27.                      
  28.  
  29.                     #region Call Razer initiate/confirm  
  30.   
  31.                     //Call Razer for initiation  
  32.                     response = await Utilities.CallRazer(gameRequest, "purchaseinitiation");  
  33.   
  34.                     //Read response  
  35.                     var htmlResponse = await response.Content.ReadAsStringAsync();  
  36.                     var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);  
  37.   
  38.                     //Adding initiation response into database  
  39.                     _unitOfWork.GameResponseRepository.Insert(gameResponse);  
  40.                       
  41.   
  42.                     if (gameResponse.initiationResultCode == "00")  
  43.                     {  
  44.                         gameRequest.validatedToken = gameResponse.validatedToken;  
  45.                         //Create signature  
  46.                         var gameConfirmRequest = Utilities.CreateSignature(gameRequest, RequestType.Confirm);  
  47.   
  48.                         //Transform DTO into GameRequest for calling Razer Initiate  
  49.                         var gameConfirmRequests = iMapper.Map<GameRequest, GameConfirmRequest>(gameConfirmRequest);  
  50.   
  51.                         //Add confirm request into database  
  52.                         _unitOfWork.GameConfirmRequestRepository.Insert(gameConfirmRequests);  
  53.                           
  54.   
  55.                         //Call Razer for confirm  
  56.                         response = await Utilities.CallRazer(gameRequest, "purchaseconfirmation");  
  57.   
  58.                         //Read response  
  59.                         htmlResponse = await response.Content.ReadAsStringAsync();  
  60.                         var gameConfirmResponse = JsonConvert.DeserializeObject<GameConfirmResponse>(htmlResponse);  
  61.   
  62.                         //Add confirm response into database  
  63.                         _unitOfWork.GameConfirmResponseRepository.Insert(gameConfirmResponse);  
  64.                     }  
  65.                 }  
  66.                 catch (Exception e)  
  67.                 {  
  68.                     throw e;  
  69.                 }  
  70.  
  71.                 #endregion  
  72.   
  73.                 await _unitOfWork.SaveAsync();  
  74.                 scope.Complete();  
  75.             }  
  76.   
  77.             return response;  
  78.         } 
 

Answers (1)