简体   繁体   English

实体与身份用户关联时,实体框架核心 3.2 更新失败

[英]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'.这是异常注释 System.InvalidOperationException HResult=0x80131509 Message=在尝试将实体的 state 更改为“Modified”时,实体类型“Medication”的属性“Id”具有临时值的详细信息。 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 Z0D61F8370CAD1D412F80B84D143E125 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:Z0D61F8370CAD1D412F80B84D143E125 中 MedicalManager.Controllers.HomeController.Edit(药物药物)的第 71 行7Z:\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() 7Z:\bite-us\stackoverflow\mmm_development\MedicalManager\Controllers\HomeController.cs:Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor 的 Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) 的第 123 行.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()

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

You should check the source of the medication ,and add the Id you need to update into the medication .您应该检查medication的来源,并将您需要更新的Id添加到medication中。

You can see my test here.你可以在这里看到我的测试。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM