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

标签 c# .net

当我尝试使用 Db.SaveAsync() 保存上下文时,它会给出如图所示的错误
我有一个循环中的函数 QuickSave(),它首先调用并保存数据,然后修改上下文并尝试保存上下文。当我保存上下文时,出现上述错误。对我来说,调用存储过程后保存上下文非常重要。

Description of the problem

这是我的代码:

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;
}

最佳答案

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;
    }

您正在使用普通方法并使用 db.Async()。因此,请使您的 api 也为 async()。尝试此代码。祝您好运。

关于c# - 错误: New transaction is not allowed because there are other threads running in the session in c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44718227/

相关文章:

c# - Windows 10 移动版不遵守 KeyboardFlags.None

c# - netTCP 绑定(bind) Soap 安全协商失败

c# - 将子实体移动到新的父实体

c# - Linq:只为 Where 和 OrderBy 调用相同的方法一次而不是两次?

c# - 调用DacService.Deploy后如何取消和回滚升级包?

c# - 创建不可见的用户控件

c# - 使用 msbuild 执行文件系统发布配置文件

c# - 获取登录用户 SID 而不是提升的用户

c# - 处理对象时是否应该释放已加载的模块?

c# - 在 Nest Elastic Search 中按降序获取 Distinct Term