SQL Server administration and T-SQL development, Web Programming with ASP.NET, HTML5 and Javascript, Windows Phone 8 app development, SAP Smartforms and ABAP Programming, Windows 7, Visual Studio and MS Office software

Development Forums for Programmer, Administrators and Users

Development resources, articles, tutorials, code samples and tools and downloads for ASP.Net, SQL Server, R Script, Windows, Windows Phone, AWS, SAP HANA and ABAP, like SAP UI5, Screen Personas, etc.

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

Last post 08-28-2009, 4:00 AM by eralper. 0 replies.
Sort Posts:
  •  08-28-2009, 4:00 AM 12805

    An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

    While using Attach method of the ContextDataAccessManager object in order to attach a detached context I got the following error message with the InvalidOperationException error is thrown by the compiler.


    An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.


    I tried to use the EntityState of the object data classes but the EntityState was already displaying the state as Detached
    While checking web for a solution I came to a solution with ObjectStateManager TryGetObjectStateEntry Method from MSDN site.

    I changed the source code within my application from the original .NET code :

    ctxManager.Attach(_context)

    to the following corrected source code :

    Dim stateEntry As ObjectStateEntry = Nothing
    If Not ctxManager.ObjectStateManager.TryGetObjectStateEntry(_context.EntityKey, stateEntry) Then
    ctxManager.Attach(_context)
    End If


    It is working now without the error message: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.

    You can find more info for sample on ObjectStateManager.TryGetObjectStateEntry at MSDN.
View as RSS news feed in XML
Copyright © 2004 - 2019 Eralper YILMAZ. All rights reserved.
Community Server by Telligent Systems