I am working on Web API using Entity Framework and C#. I need solution for dirty reads.
I have tried below method and also transaction method, I need solution for dirty reads.
Dirty reads or phantom reads is a phenomenon where a user is not updating the latest data.
Let's say user A has opened salesman web page
user B also opened the same salesman web page
A has loaded salesman ID 1001 and B also loaded 1001 A changed salesman name to X and saved.
But B is seeing old data and if B changes the salesman name to Y, A's changes will be overwritten. So we should prevent B from writing the changes to DB.
So I need solution based on the above concept.
using (var transaction = db.Database.BeginTransaction())
{
try
{
db.SaveChanges();
transaction.Commit();
}
catch (Exception excp)
{
throw excp;
}
}
return Ok();
Below is the code that I have tried
using (var transaction = db.Database.BeginTransaction())
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
KSTU_COUNTER_MASTER kcm = new KSTU_COUNTER_MASTER();
kcm.obj_id = Common.GetNewGUID();
kcm.company_code = Common.CompanyCode;
kcm.branch_code = Common.BranchCode;
kcm.counter_code = c.CounterCode;
kcm.counter_name = c.CounterName;
kcm.Maincounter_code = c.MaincounterCode;
kcm.obj_status = c.ObjectStatus;
kcm.UpdateOn = Framework.Common.GetDateTime();
kcm.UniqRowID = Guid.NewGuid();
db.KSTU_COUNTER_MASTER.Add(kcm);
try
{
db.SaveChanges();
transaction.Commit();
}
catch (Exception excp)
{
throw excp;
}
}
return Ok();