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?
- 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:
- using System;
-
- namespace BusinessEntity
- {
- public class GameResponse
- {
- public int Id { get; set; }
- public string referenceId { get; set; }
- public string productCode { get; set; }
- public int quantity { get; set; }
- public string version { get; set; }
- public string signature { get; set; }
- public string ApplicationCode { get; set; }
- public string validatedToken { get; set; }
- public DateTime? responseDateTime { get; set; } = DateTime.Now;
- public string initiationResultCode { get; set; }
- public string companyToken { get; set; }
- public string estimateUnitPrice { get; set; }
- public string currency { get; set; }
- }
- }
Here is the method I am load testing:
- private async Task<HttpResponseMessage> CallRazerService(RequestDto requestDto)
- {
- HttpResponseMessage response = null;
-
- using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
- {
- try
- {
-
- var config = new MapperConfiguration(cfg =>
- {
- cfg.CreateMap<RequestDto, GameRequest>();
- cfg.CreateMap<GameRequest, GameConfirmRequest>();
- cfg.CreateMap<GameConfirmResponse, GameConfirmResponseDto>();
- cfg.CreateMap<Coupon, CouponDto>();
- });
- var iMapper = config.CreateMapper();
- var gameRequest = iMapper.Map<RequestDto, GameRequest>(requestDto);
-
- gameRequest.referenceId = Guid.NewGuid().ToString();
-
-
- gameRequest = Utilities.CreateSignature(gameRequest, RequestType.Initiate);
-
-
- _unitOfWork.GameRepository.Insert(gameRequest);
-
-
- #region Call Razer initiate/confirm
-
-
- response = await Utilities.CallRazer(gameRequest, "purchaseinitiation");
-
-
- var htmlResponse = await response.Content.ReadAsStringAsync();
- var gameResponse = JsonConvert.DeserializeObject<GameResponse>(htmlResponse);
-
-
- _unitOfWork.GameResponseRepository.Insert(gameResponse);
-
-
- if (gameResponse.initiationResultCode == "00")
- {
- gameRequest.validatedToken = gameResponse.validatedToken;
-
- var gameConfirmRequest = Utilities.CreateSignature(gameRequest, RequestType.Confirm);
-
-
- var gameConfirmRequests = iMapper.Map<GameRequest, GameConfirmRequest>(gameConfirmRequest);
-
-
- _unitOfWork.GameConfirmRequestRepository.Insert(gameConfirmRequests);
-
-
-
- response = await Utilities.CallRazer(gameRequest, "purchaseconfirmation");
-
-
- htmlResponse = await response.Content.ReadAsStringAsync();
- var gameConfirmResponse = JsonConvert.DeserializeObject<GameConfirmResponse>(htmlResponse);
-
-
- _unitOfWork.GameConfirmResponseRepository.Insert(gameConfirmResponse);
- }
- }
- catch (Exception e)
- {
- throw e;
- }
-
- #endregion
-
- await _unitOfWork.SaveAsync();
- scope.Complete();
- }
-
- return response;
- }