读取ID的信息,导致异常出现错误

发布于 2025-01-28 10:16:25 字数 1461 浏览 2 评论 0原文

在C#我的SQL实体框架6插入是返回异常

无法添加或更新子行:外键约束失败 (portaldbMasterselection Himentory,约束 fk_masterselection history_contacts_masterid外键(MasterID) 参考MastersID)删除级联)

发生了例外,试图将记录列表插入数据库时​​,

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, CONSTRAINT
FK_MasterSelectionHistory_Contacts_MasterId FOREIGN KEY (MasterId)
REFERENCES masters (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文