Developing a project using WebAPI and Entity Framework 6.
There is a controller with a method that adds a new Task entity to the database. At Task before adding the unique name is calculated. Part of the name is created on the basis of already saved Task in the database. If you make two parallel queries, you can generate the same name, this problem is solved through lock.
How to prevent the generation of identical names if two WebAPI servers running on one database are running?
Can I add an algorithm to add a Task to a serial queue at the database level?
Simplified code:
[RoutePrefix("api/tasks")] public class TaskController1 : ApiController { [HttpPost] [Route("add")] public IHttpActionResult AddTask() { using (var db = CreateDbContext()) { Task task = new Task(); lock (lockAdd) { DateTime dateTime = DateTime.UtcNow; string preName = dateTime.ToString("yyMMdd-HHmm-"); var query = db.Tasks.Where(t => t.Name.StartsWith(preName)).Select(t => t.Name); List<string> tasksNames = query.ToList(); task.Name = preName + (tasksNames.Count + 1).ToString("D4"); db.Tasks.Add(task); db.SaveChanges(); } return Ok(); } } }
tasksNames.Countis the number of tasks with names in one date. But then if you add 3 records to the database and then delete the first one, your algorithm will give Count = 2, which will create the name date-3, but the record 3 is already there ... - Mikeselect count(1) from (select * from Task where name like 'дата%' for update) Xthen all records with the specified date would be blocked and another process in the database would wait for your transaction to complete . The truth remains the problem with the first record, when there is nothing more to block ... - Mike