2014-01-06 40 views
0

我在表中特定条目的DBUpdation异常异常期间收到错误。在更新实体框架时出现错误

无法添加或更新子行:外键约束失败,但USerID存在于appuser表中。

首先我将其中一列更新为false,然后为表中的相同用户ID添加一个新条目。相同的代码在本地环境中工作正常。

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) 
+0

为了帮助我认为你需要显示什么实体失败和表结构。展示你提供的价值观也会给人更清晰的印象。 –

+0

如果您有错误处理程序,请在其中放入一个中断并查看异常的详细信息。 EF非常擅长将异常数据中的FK失败。 –

+0

向我们展示一些代码 – Colin

回答

1

这里最重要的信息是:

  • 在本地工作
  • 错误是与APPUSER

因此问题可能是一个外键约束:

  • 关于远程服务使用未在远程数据库中注册的appuser访问用户。
+0

感谢它得到解决..我试图更新和删除同一行。因此,这个问题。标记为答案,因为堆栈跟踪确实帮助我找到答案。 – TBA