Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
.NET
.NET Core
.NET MAUI
.NET Standard
Active Directory
ADO.NET
Agile Development
AI
AJAX
AlbertAGPT
Alchemy
Alexa Skills
Algorand
Algorithms in C#
Android
Angular
ArcObject
ASP.NET
ASP.NET Core
Augmented Reality
Avalanche
AWS
Azure
Backbonejs
Big Data
BizTalk Server
Blazor
Blockchain
Bootstrap
Bot Framework
Business
Business Intelligence(BI)
C#
C# Corner
C# Strings
C, C++, MFC
Career Advice
Careers and Jobs
Chapters
ChatGPT
Cloud
Coding Best Practices
Cognitive Services
COM Interop
Compact Framework
Copilot
Cortana Development
Cosmos DB
Cryptocurrency
Cryptography
Crystal Reports
CSS
Current Affairs
Custom Controls
Cyber Security
Data Mining
Data Science
Databases & DBA
Databricks
Design Patterns & Practices
DevExpress
DevOps
DirectX
Dynamics CRM
Enterprise Development
Entity Framework
Error Zone
Exception Handling
F#
Files, Directory, IO
Flutter
Games Programming
GDI+
General
Generative AI
GO
Google Cloud
Google Development
Graphics Design
Graphite Studio
Hardware
Hiring and Recruitment
HoloLens
How do I
HTML 5
Infragistics
Internet & Web
Internet of Things
Ionic
Java
Java and .NET
JavaScript
JQuery
JSON
JSP
Knockout
Kotlin
Langchain
Leadership
Learn .NET
Learn iOS Programming
LINQ
Machine Learning
Metaverse
Microsoft 365
Microsoft Fabric
Microsoft Office
Microsoft Phone
Microsoft Teams
Mobile Development
MongoDB
MuleSoft
MySQL
NEAR
NetBeans
Networking
NFT
NoCode LowCode
Node.js
Office Development
OOP/OOD
Open Source
Operating Systems
Oracle
Outsourcing
Philosophy
PHP
Polygon
PostgreSQL
Power Apps
Power Automate
Power BI
Power Pages
Printing in C#
Products
Progress
Progressive Web Apps
Project Management
Public Speaking
Python
Q#
QlikView
Quantum Computing
R
React
React Native
Reports using C#
Robotics & Hardware
RPA
Ruby on Rails
RUST
Salesforce
Security
Servers
ServiceNow
SharePoint
Sharp Economy
SignalR
Smart Devices
Snowflake
Software Architecture/Engineering
Software Testing
Solana
Solidity
Sports
SQL
SQL Server
Startups
Stratis Blockchain
Swift
SyncFusion
Threading
Tools
TypeScript
Unity
UWP
Visual Basic .NET
Visual Studio
Vue.js
WCF
Wearables
Web API
Web Design
Web Development
Web3
Windows
Windows Controls
Windows Forms
Windows PowerShell
Windows Services
Workflow Foundation
WPF
Xamarin
XAML
XML
XNA
XSharp
Register
Login
1
Answer
The changes to the database were committed successfully, but
Cenk
5y
8.9k
1
Reply
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
{
//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>();
});
var iMapper = config.CreateMapper();
var gameRequest = iMapper.Map<RequestDto, GameRequest>(requestDto);
//Unique reference ID
gameRequest.referenceId = Guid.NewGuid().ToString();
//Create signature
gameRequest = Utilities.CreateSignature(gameRequest, 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"
)
{
gameRequest.validatedToken = gameResponse.validatedToken;
//Create signature
var gameConfirmRequest = Utilities.CreateSignature(gameRequest, 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(gameRequest,
"purchaseconfirmation"
);
//Read response
htmlResponse = await response.Content.ReadAsStringAsync();
var gameConfirmResponse = JsonConvert.DeserializeObject<GameConfirmResponse>(htmlResponse);
//Add confirm response into database
_unitOfWork.GameConfirmResponseRepository.Insert(gameConfirmResponse);
}
}
catch
(Exception e)
{
throw
e;
}
#endregion
await _unitOfWork.SaveAsync();
scope.Complete();
}
return
response;
}
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Routing in asp.net
App won’t work correctly when set to run at boot.