0

When I am adding an entry

var objectEntity = new ObjectStaging();
objectEntity .CreatedBy = ClaimsPrincipal.Current.Identity.Name;
objectEntity .ModifiedBy = ClaimsPrincipal.Current.Identity.Name;
objectEntity .Set(ObjectInfo);
database.ObjectStaging.Add(objectEntity);
database.SaveChanges();

For some reason, my objectEntity is not being added to my database given the following code. When using the debugger, it moves through database.ObjectStaging.Add(objectEntity); but does not continue after calling SaveChanges(). Is there a way to know why it is failing to add the entry to the database?

Exception Thrown:

Exception thrown: 'System.Data.Entity.Validation.DbEntityValidationException' in VCC.BrokerPortal.DAO.dll Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Is there a way to know what specifically did not validate?

Also, I tested the state of the entity using the following lines of code,

database.ObjectStaging.Add(objectEntity);
var state= brokerPortalDB.Entry(objectEntity).State;

state currently shows the value of 'added'.

Danny
  • 63
  • 5
  • 1
    have you check database.Entry(objectEntity).State ? – FLICKER Feb 19 '20 at 00:10
  • If the debugger does not proceed past SaveChanges() then you likely have an exception being raised and your exception settings aren't to break on all exceptions or this code is operating on a worker thread swallowing the exception without the debugger breaking. Exception settings should be available in VS under "Debug"->"Windows" then select the "Common Language Runtime Exceptions" so it becomes a ticked box. (not the default blocked "Square") – Steve Py Feb 19 '20 at 00:43
  • @FLICKER - Updated the thread. It is showing a value of 'added' when I test the state. – Danny Feb 19 '20 at 17:49
  • See this link to explore validation erros: https://stackoverflow.com/questions/7795300/validation-failed-for-one-or-more-entities-see-entityvalidationerrors-propert – FLICKER Feb 19 '20 at 17:58

1 Answers1

0

Thank you!

I ended up looking into the exception thrown and it appears it was related to one of the properties not having proper validation. In this instance I had a [MaxLength] attribute assigned to a field and I was trying to add a value longer than that length allowed.

Danny
  • 63
  • 5