我有一种情况,我必须将记录插入表中,获取插入记录的ID并更新第二个表(具有其他一些属性)
这是代码示例:
public void UpdateLookupDetails()
{
string newId;
{
//Add new record
newId = Guid.NewGuid().ToString();
var newRefUiRecord = new Entities.REFUILBLTXT
{
ID = newId,
ACTIVEFLG = Constant.Flag.Yes,
CRTTS = DateTime.Now,
CRTUID = "cfbc94a9-16a5-11e1-8a27-a2e9ab9c0b63",
LBLTXT = "Description",
LOCALECD = Constant.LanguageCode.English,
MODTS = DateTime.Now,
MODUID = "cfbc94a9-16a5-11e1-8a27-a2e9ab9c0b63",
LBLCD = "LookupList.DESC"
};
_context.REFUILBLTXTs.Add(newRefUiRecord);
}
var sample = _context.ZREFLISTs.FirstOrDefault(x => x.ID == "e8ae24b1-029c-44d2-a413-c25c19189751");
_context.ZREFLISTs.Attach(sample);
sample.REFLISTDESCLBLID = newId;
_context.Entry(sample).Property(x => x.REFLISTDESCLBLID).IsModified = true;
_context.SaveChanges();
}
截至目前,应用程序停留在_context.SaveChanges();
上,大约一分钟后从Integration Test用例运行时,它会给出错误提示
TaskCanceledException: A task was canceled.
如果我注释掉_context.REFUILBLTXTs.Add(newRefUiRecord);
OR _context.ZREFLISTs.Attach(sample);
,则可以!因此,它一次只能更新或插入记录。
任何人都可以了解这里的问题吗?
环境:Web API 2 + Oracle EF +代码优先
答案 0 :(得分:1)
您可以删除 attach 语句并尝试如下操作吗:
var sample = _context.ZREFLISTs.FirstOrDefault(x => x.ID == "e8ae24b1-029c-44d2-a413-c25c19189751");
sample.REFLISTDESCLBLID = newId;
_context.Entry(sample).State = EntityState.Modified;