当我尝试使用Db.SaveAsync()保存上下文时,它会像图像中那样给出错误
我在循环中有一个函数QuickSave(),它首先调用和保存数据然后修改上下文并尝试保存上下文。当我保存上下文时,我遇到了错误。对我来说,在调用存储过程之后保存上下文非常重要。
这是我的代码:
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;
}
答案 0 :(得分:2)
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()。尝试这个代码。运气好。