首页 > 解决方案 > 实体与身份用户关联时,实体框架核心 3.2 更新失败

问题描述

这是一个新问题。试图更新数据库中的条目。注意,这个条目依赖于用户。当我没有与该项目没有任何问题的用户关联时,我执行了相同的逻辑。然而,现在它让我感到悲伤。这是屏幕截图:

在此处输入图像描述

以下是异常说明的详细信息:

System.InvalidOperationException
  HResult=0x80131509
  Message=The property 'Id' on entity type 'Medication' has a temporary value while attempting to change the entity's state to 'Modified'. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.
  Source=Microsoft.EntityFrameworkCore
  StackTrace:
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState oldState, EntityState newState, Boolean acceptChanges, Boolean modifyProperties)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.SetEntityState(EntityState entityState, Boolean acceptChanges, Boolean modifyProperties, Nullable`1 forceStateWhenUnknownKey)
   at Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry.set_State(EntityState value)
   at MedicalManager.Models.Repositories.MedicationRepository.UpdateMedication(Medication medication, String UserId) in C:\bite-us\stackoverflow\mmm_development\MedicalManager\Models\Repositories\MedicationRepository.cs:line 71
   at MedicalManager.Controllers.HomeController.Edit(Medication medication) in C:\bite-us\stackoverflow\mmm_development\MedicalManager\Controllers\HomeController.cs:line 123
   at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync()
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync()

标签: c#asp.net-mvcasp.net-coretracker-enabled-dbcontext

解决方案


因为你的关系是一对多的,如果你的药物不包含Id,它不知道要更新哪些数据,就会导致这个问题。

您应该检查 的来源medication,并将Id您需要更新的内容添加到medication.

你可以在这里看到我的测试。 在此处输入图像描述


推荐阅读