无法跟踪实体类型“ Item”的实例,因为已经跟踪了{'ItemID'}具有相同键值的另一个实例

时间:2020-03-26 06:30:06

标签: c# entity-framework-core linq-to-entities

在更新我尝试使用context.update,entity.statehaschanged的实体时发生异常

无法跟踪实体类型“ Item”的实例,因为已经跟踪了另一个具有相同“ {'ItemID'}”键值的实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。考虑使用'DbContextOptionsBuilder。

从视图代码调用功能

_reagent =reagentService.GetReagents(_formula.ReagentsID);
              _reagent.Quantity = _reagent.Quantity - reagentQuantityUsed;
              reagentService.EditReagents(_reagent);

实体代码:

> public Reagents GetReagentsDetails(int id)
>         {
>           Reagents reagent = new Reagents();
>           try
>           {
>             reagent = labOrgDbContext.Reagents.Include(x => x.ReagentCategory).SingleOrDefault(x => x.ReagentsID == id &&
> x.IsDeleted != true);
>           }
>           catch (Exception ex)
>           {
>     
>             ExceptionLogging.SendErrorToText(ex, "1");
>           }
>           return reagent;
>         }
>         public void UpdateReagents(Reagents reagent)
>         {
>           try
>           {
>             //labOrgDbContext(reagent).State = EntityState.Modified;
>             labOrgDbContext.Entry(reagent).CurrentValues.SetValues(reagent);
>            // labOrgDbContext.Update(reagent);
>             labOrgDbContext.SaveChanges();
>           }
>           catch (Exception ex)
>           {
>             ExceptionLogging.SendErrorToText(ex, "1");
>           }
>         }

发生错误**************** ******************

0 个答案:

没有答案