0

I am getting an error during DBUpdation Exception exception of a particular entry in the table.

Cannot add or update a child row: a foreign key constraint fails, however the USerID exist in appuser table.

First I will update one of the column to false, then add a new entry to the table for the same userID. The same code is working fine in local environment.

System.Data.Entity.Infrastructure.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> MySql.Data.MySqlClient.MySqlException: Cannot add or update a child row: a foreign key constraint fails (`asd34556`.`freeattribute`, CONSTRAINT `FK_FreeAttr_UserID_ApUser_UserID` FOREIGN KEY (`UserID`) REFERENCES `appuser` (`UserID`) ON DELETE NO ACTION ON UPDATE NO ACTION)
at MySql.Data.MySqlClient.MySqlStream.ReadPacket()
at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId)
at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force)
at MySql.Data.MySqlClient.MySqlDataReader.NextResult()
at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)
at MySql.Data.Entity.EFMySqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
--- End of inner exception stack trace ---
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
at System.Data.Entity.Internal.InternalContext.SaveChanges()
--- End of inner exception stack trace ---
at System.Data.Entity.Internal.InternalContext.SaveChanges()
at FreeLance.Business.ProfileManagement.ProfileManagementBC.UpdateOtherDetails(appuser user)
at FreeLance.Web.Controllers.ManageProfileController.OtherInfoUpdate(ProfileModel model)
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
4
  • In order to help I think you need to show what entity failed and the table structure. Showing what values you provided would give a clearer picture also. Commented Jan 6, 2014 at 14:22
  • If you have an error handler, put a break in it and look at the detail of the exception. EF is pretty good at putting which FK failed in the exception data. Commented Jan 6, 2014 at 14:34
  • Show us some code please Commented Jan 6, 2014 at 21:09
  • @Colin simon The issue got resolved as the entity was trying to update and delete the same row Indeed. Separated too logic and got this working. Sorry for not putting the code. Commented Jan 7, 2014 at 13:32

1 Answer 1

1

Here the important info is:

  • It Works locally
  • The error is a Foreign key constraint related to the Appuser

Therefore problem probably is:

  • On the remoted server you accessing the user using an appuser that is not registered in the Remote database.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it got solved.. I was trying to update and delete the same row. Hence the problem. marking as answer because the Stack Trace did help me to find the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.