gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:11:23 24 4
gpt4 key购买 nike

当我尝试使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com