I have this code that tries to save the model in the database and makes two catch one for the validations of EF and the other generic. It does not give me any error of the first catch but in the second it gives me an error of a foreign key I think, but I do not see how to solve it.
try
{
db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
// Join the list to a single string.
var fullErrorMessage = string.Join("; ", errorMessages);
// Combine the original exception message with the new one.
var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
string error = exceptionMessage;
// Throw a new DbEntityValidationException with the improved exception message.
throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
}
catch (Exception ex)
{
string error2 = ex.InnerException.InnerException.Message;
}
The error I receive is
INSERT statement in conflict with the FOREIGN KEY constraint "FK_VentasLinias_Articulos". The conflict has appeared in the database "testweb", table "dbo.Articulos". The instruction was finished.
Some captures of the relationship that I have defined in the diagram of the BD
Thank you,