Asp.Net Web API Entityframework Concurrency RRS feed

  • Question

  • 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:

        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();
        public async Task<int> UpdateStartlists(int eventid, List<StartlistEntry> en)
                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);
                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

    • Moved by Kristin Xie Friday, January 16, 2015 2:34 AM
    Thursday, January 15, 2015 5:29 PM


  • Hi Manu,

    Like your title mentioned, your case related to Web API, please redirect to Asp.Net Web API forum.

    Here  is the link:


    Thanks for your understanding.

    Best regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Just Karl Tuesday, January 20, 2015 9:52 PM
    • Marked as answer by Just Karl Friday, April 17, 2015 8:05 PM
    Friday, January 16, 2015 2:33 AM