Hi,
i have following Scenario: I have 2 web api functions, which delete / insert data into a SQL Server database. The data Access is handeld via .net entityframework v6. The insert / delete methods were only called from a local running c# program. I am using
HttpClient class to call the web api methods. The web methods works as follows, when i call insert all existing records will be deleted and the new ones will be inserted, so there is no real update process.
Here are my 2 functions:
[HttpDelete()]
public async Task<int> DeleteStartlist(int eventid, int run, int heat, string category)
{
_data.dbsStartlistEntries.RemoveRange(_data.dbsStartlistEntries.Where(s => s.Event.Id == eventid && s.RoundOrder == run && s.HeatOrder == heat && s.Category == category));
return await _data.SaveChangesAsync();
}
[HttpPost()]
public async Task<int> UpdateStartlists(int eventid, List<StartlistEntry> en)
{
try
{
if (en.Count == 0)
return 0;
var xdel = await DeleteStartlist(eventid, en[0].RoundOrder, en[0].HeatOrder, en[0].Category);
var ev = await _data.dbsEvents.FindAsync(eventid);
if (ev != null)
{
en.ForEach(e => e.Event = ev);
_data.dbsStartlistEntries.AddRange(en);
}
return await _data.SaveChangesAsync();
}
catch (System.Exception ex)
{
return 1;
}
}
But now i have following Problem. For example when i call the Update Method 10 times in a row without waiting between the function calls i receive following exception: "Store update, insert, or delete statement affected an unexpected number of rows
(0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries." For me this sounds like a concurrency Problem, but i do not really know how to solve it. So here is my question, is there a way to let
the api calls wait for each other serverside, or are they always running concurrent or is there a way to lock the database?
Kind Regards Manu