简体   繁体   English

ObjectStateManager中已存在具有相同键的对象。 使用AsNoTracking时

[英]An object with the same key already exists in the ObjectStateManager. when using AsNoTracking

There are many errors here in SO, but this scenario I think its different. SO中有很多错误,但我认为这种情况有所不同。

I had some code that was working fine, I update one entity, etc. But the problem is that it was being cached, when I disabled caching with AsNoTracking(), then it started to throw this exception; 我有一些工作正常的代码,我更新一个实体,等等。但问题是它被缓存,当我使用AsNoTracking()禁用缓存时,它开始抛出此异常;

What I do its the following: 1. In the page I get a EcoBonusRequest. 我做了以下几点:1。在页面中,我获得了EcoBonusRequest。 2. The user clicks a button and it modifies the currentstatus proeprty, and it addsa a list item to a collection of workflowhistories, (an associated collection of the ecobonusrequestobject). 2.用户单击一个按钮,它会修改currentstatus proeprty,并将一个列表项添加到工作流历史的集合中(ecobonusrequestobject的相关集合)。 3. I call the udpate method, setting state to modified, here it throwed the exception. 3.我调用udpate方法,将状态设置为modified,这里抛出了异常。

Here is my code> 这是我的代码>

EcoBonusRequestRepository.cs EcoBonusRequestRepository.cs

public void UpdateEcoBonusRequest(EcoBonusRequest ecoBonusRequest)
            {
                _context.EcoBonusRequests.Attach(ecoBonusRequest);
                _context.Entry(ecoBonusRequest).State = EntityState.Modified;
            }

EcoBonusRequestBL EcoBonusRequestBL

public void Update(EcoBonusRequest ecoBonusRequest)
        {
            DALFacade.UpdateEcoBonusRequest(ecoBonusRequest);
        }

EcoBonusRequest Page EcoBonusRequest

public void ChangeStatus(EcoBonusRequest ecoBonusRequest, string stepname, string who, string choosenoption)
        {

            ecoBonusRequest.WorkflowHistories = new List<WorkflowHistory>
                                                   {
                                                       new WorkflowHistory()
                                                           {
                                                               Date = DateTime.Now,
                                                               What = choosenoption,
                                                               StepName = stepname,
                                                               Who = who
                                                           }
                                                   };

            ecoBonusRequest.CurrentStatus = _currentStepBlo.StepName;
            var ecoBonusRequestBL = new EcoBonusRequestBL();
            ecoBonusRequestBL.Update(ecoBonusRequest);
        }

This is how I get the object first: (I am not going to paste all code from all layers here) 这是我首先获得对象的方式:(我不会在这里粘贴所有图层的所有代码)

protected override void OnInit(EventArgs e)
        {
            var requestBaseId = RequestBaseId;
            if (requestBaseId != null)
            {
                EcoBonusRequest request = GetDBRequest(requestBaseId.Value);

In the dal 在达尔

public IQueryable<EcoBonusRequest> FindEcoBonusRequests(System.Linq.Expressions.Expression<Func<EcoBonusRequest, bool>> predicate)
            {
                return _context.EcoBonusRequests.AsNoTracking().Where(predicate);
            }

Yeah this is a fun one, the issue here comes from trying to re-attach an entity which is already attached to the context. 是的,这是一个有趣的问题,这里的问题来自于试图重新附加已经附加到上下文的实体。 This can come from a logic error or perhaps you are reusing your context without clearing the attached objects. 这可能来自逻辑错误,或者您可能在不清除附加对象的情况下重用上下文。 There is a local collection on the database which you can use to see what items are currently attached to the context. 数据库上有一个本地集合,您可以使用它来查看当前附加到上下文的项目。

ctx.YourEntityCollection.Local

暂无
暂无

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

相关问题 LINQ初始化器引发“对象状态管理器中已经存在具有相同键的对象。” - LINQ Initializer Throws a “An object with the same key already exists in the ObjectStateManager.” 错误ObjectStateManager中已经存在具有相同键的对象。 使用ViewModel - Error An object with the same key already exists in the ObjectStateManager. with ViewModel {“ ObjectStateManager中已经存在具有相同键的对象。 ObjectStateManager无法使用相同的键跟踪多个对象。”} - {“An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.”} 具有相同键的对象已存在于ObjectStateManager中。 ObjectStateManager无法使用相同的键跟踪多个对象 - An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key 错误:ObjectStateManager中已经存在具有相同键的对象。 ObjectStateManager无法使用相同的键跟踪多个对象 - Error : An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key 错误:ObjectStateManager中已经存在具有相同键的对象。 ObjectStateManager无法使用相同的键跟踪多个对象 - Error: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key 新对象,但错误:对象状态管理器中已经存在具有相同键的对象。 objectstatemanager无法跟踪多个(…) - New object, but error: an object with the same key already exists in the objectstatemanager. the objectstatemanager cannot track multiple (…) 实体框架“ObjectStateManager 中已存在具有相同密钥的 object。” - 我不明白如何或从这里做什么 - Entity Framework “An object with the same key already exists in the ObjectStateManager.” - I can't understand how, or what to do from here 具有相同键的对象已存在于ObjectStateManager中 - An object with the same key already exists in the ObjectStateManager ObjectStateManager中已存在具有相同键的对象 - An object with the same key already exists in the ObjectStateManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM