Kaval Patel
Kaval Patel

Reputation: 668

Error : New transaction is not allowed because there are other threads running in the session in c#

when i am trying save context using Db.SaveAsync() it will give error like in the image
I have a function QuickSave() in loop, which calls and the save data first and then modify context and tries to save the context. When I'm saving the context I'm getting above error. For me, it is important to save the context after calling stored procedure.

Description of the problem

Here is my code:

var items = StoryTaskCount(item.ProjectId, storyList);

//var st = items.FirstOrDefault();

foreach(var st in items) {
    var story = await Factory.StoryService.StoryByIdAsync(item.ProjectId, st.Id);
    if (st.Count == 0) {
        taskItem = await Factory.TaskService.QuickSave(item.ProjectId, story.StoryId, "Task for " + ConstantCompany.Prefix.StoryCode + story.StoryCode, true);
        story.TaskId = taskItem.TaskId;
    }
    story.TaskCount = st.Count;
}

public IQueryable < BSTChildVM > StoryTaskCount(decimal projectId, string ids) {
    var res = (from story in Factory.StoryService.ForProject(projectId) 
                join st in Factory.BusinessHelperService.GetIds(ids) on story.StoryId equals st
                join task in Factory.TaskService.AllTasks(projectId) on story.StoryId equals task.StoryId 
                into ftask from task in ftask.DefaultIfEmpty() 
                group task by story.StoryId into taskGroup select new BSTChildVM 
                {
                    Id = taskGroup.Key,
                    Count = (from item in taskGroup where item.TaskId != null select item).Count()
                }
            );
    return res;
}

Upvotes: 2

Views: 295

Answers (1)

Chirag Patel
Chirag Patel

Reputation: 373

var items = await StoryTaskCount(item.ProjectId, storyList);

       //var st = items.FirstOrDefault();
        foreach (var st in items)
        {
            var story = await Factory.StoryService.StoryByIdAsync(item.ProjectId, st.Id);
            if (st.Count == 0)
            {
                taskItem = await Factory.TaskService.QuickSave(item.ProjectId, story.StoryId, "Task for " + ConstantCompany.Prefix.StoryCode + story.StoryCode, true);
                story.TaskId = taskItem.TaskId;
            }
            story.TaskCount = st.Count;
        }
public async Task<List<BSTChildVM>> StoryTaskCount(decimal projectId, string ids)
    {
        var res = await (from story in Factory.StoryService.ForProject(projectId)
                         join st in Factory.BusinessHelperService.GetIds(ids) on story.StoryId equals st
                         join task in Factory.TaskService.AllTasks(projectId) on story.StoryId equals task.StoryId into ftask
                         from task in ftask.DefaultIfEmpty()
                         group task by story.StoryId into taskGroup
                         select new BSTChildVM
                         {
                             Id = taskGroup.Key,
                             Count = (from item in taskGroup
                                      where item.TaskId != null
                                      select item).Count()
                         }).ToListAsync();
        return res;
    }

you are using normal method and use db.Async().so,please make your api also async().try this code.Best of Luck.

Upvotes: 2

Related Questions