提问人:Sebastian 提问时间:5/13/2022 更新时间:5/13/2022 访问量:191
读取导致异常错误的 ID 信息
Read information of ID causing error from Exception
问:
在 C# 中,我的 Sql Entity Framework 6 插入是返回异常
无法添加或更新子行:外键约束失败 (., 约束外键 () 删除级联时引用 ()
portaldb
masterselectionhistory
FK_MasterSelectionHistory_Contacts_MasterId
MasterId
masters
Id
尝试将记录列表插入数据库时发生异常
try{
List<int> masterIds = // around 20k ID exists
foreach (int id in masterIds)
{
//Add Selection History to record against the contact!
#region insert to ContactSelectionHistory
MasterSelectionHistory dbEntry = new MasterSelectionHistory();
dbEntry.MasterId = id;
dbEntry.SelectionId = --;
dbEntry.DateCreated = DateTime.Now;
dbEntry.CreatedBy = ---;
context.MasterSelectionHistory.Add(dbEntry);
#endregion
}
//Save all the db context changes
await context.SaveChangesAsync().ConfigureAwait(false); //EXception is throwing from here
}
catch (Exception ex)
{
//Can read ID causing error from ex object ?
throw new InvalidOperationException(ex.Message);
}
有没有办法知道哪个 ID 导致了异常对象的问题
答: 暂无答案
评论