读取ID的信息,导致异常出现错误
在C#我的SQL实体框架6插入是返回异常
无法添加或更新子行:外键约束失败 (
portaldb
。Masterselection Himentory
,约束fk_masterselection history_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中的哪个正在引起ID中的哪个来自异常对象的问题
In C# My Sql Entity Framework 6 Insert is return exception
Cannot add or update a child row: a foreign key constraint fails
(portaldb
.masterselectionhistory
, CONSTRAINTFK_MasterSelectionHistory_Contacts_MasterId
FOREIGN KEY (MasterId
)
REFERENCESmasters
(Id
) ON DELETE CASCADE)
The exception is occuring while trying to insert a List of records to the db
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);
}
Is there any way to know which among the ID is causing the issue from exception object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论