c# - Error while inserting duplicate records -
i have code commit
public retornodto commit(efdbcontext _context) { string errovalidation = string.empty; try { _context.savechanges(); } catch (dbentityvalidationexception e) { foreach (var eve in e.entityvalidationerrors) { var erro = string.format("entity of type \"{0}\" in state \"{1}\" has following validation errors:", eve.entry.entity.gettype().name, eve.entry.state); logger.getinstance().erro(erro); foreach (var ve in eve.validationerrors) { errovalidation = string.format("- property: \"{0}\", error: \"{1}\"", ve.propertyname, ve.errormessage); logger.getinstance().erro(errovalidation); } } return new retornodto { sucesso = false, mensagem = "error - erro " + errovalidation }; } return new retornodto { sucesso = true, mensagem = "changes saved!!!" }; }
but when try insert duplicate record catch statement doesn't work , receive error:
cannot insert duplicate key row in object 'dbo.clientedocliente' unique index 'ix_nomeclientedocliente'.the duplicate key value (cliente name test). statement has been terminated.
linha 30: try linha 31: { linha 32: _context.savechanges(); linha 33: } linha 34: catch (dbentityvalidationexception e)
what need change avoid message?
your catch catching dbentityvalidationexception
. inserting duplicate record not dbentityvalidationexception, exception.
you need add further catch
kind of exception (or catch all), or if current catch code meant issue, correct exception catching.
the error message might tell exact exception duplicate records; if not, can try find in manual, or catch higher level exception , check in debugger exact exception is.
Comments
Post a Comment