'Read information of ID causing error from Exception
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|