Bite-us
Bite-us

Reputation: 15

Failure Entity Framework Core 3.2 update while entity has association with Identity User

Here is a new issue. Trying to update entry in the Database. Note, this entry has dependency on User. I conducted the same logic I used to do when I didn't have user association with the item without any issue. However, now it is giving me grief. Here is the screenshot:

enter image description here

Here is the details on the exception note:

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()

Upvotes: 0

Views: 636

Answers (1)

Yinqiu
Yinqiu

Reputation: 7200

Because your relationship is one to many,if your medication don't include Id,It will not know which data to update.It will cause this issue.

You should check the source of the medication,and add the Id you need to update into the medication.

You can see my test here. enter image description here

Upvotes: 0

Related Questions