0

Getting this exception

How i can solve this sir, need solution to fix this

EDIT :

I open my detail and i found this. But i still not understand why The PK is doubled Detail : {"Violation of PRIMARY KEY constraint 'PK_dbo.HeadMasters'. Cannot insert duplicate key in object 'dbo.HeadMasters'. The duplicate key value is (1).\r\nThe statement has been terminated."}

enter image description here

Student
  • 91
  • 2
  • 11

1 Answers1

0

It is possible to override the SaveChanges method on the context. That will result in a more detailed error. Based on link please find an extended SaveChanges() below.

  public override int SaveChanges()
    {
        try
        {
            return base.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);

            // Throw a new DbEntityValidationException with the improved exception message.
            throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
        }
        catch (System.Data.Entity.Infrastructure.DbUpdateException ex)
        {
            string error = string.Format("Message: {0}, InnerException: {1}",
                                 ex.Message,
                                 (ex.InnerException != null ? ex.InnerException.ToString() : "")
                                 );

            throw new Exception("DbUpdateException: " + error);
        }
        catch (System.Data.Entity.Core.UpdateException ex)
        {
            string error = string.Format("Message: {0}, InnerException: {1}",
                               ex.Message,
                               (ex.InnerException != null ? ex.InnerException.ToString() : "")
                               );

            throw new Exception("UpdateException: " + error);

        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string error = string.Format("Message: {0}, InnerException: {1}, SqlErrorNumber: {2}, StackTrace: {3}",
                ex.Message,
                (ex.InnerException != null ? ex.InnerException.Message : ""),
                ex.Number.ToString(),
                ex.StackTrace.ToString()
                );

            throw new Exception("SqlException: " + error);
        }
        catch
        {
            throw;
        }
    }
Community
  • 1
  • 1
masterw
  • 116
  • 4