Thread is closed for posting
3 posts, 0 answers
  1. Armin
    Armin avatar
    11 posts
    Member since:
    Feb 2011

    Posted 03 Feb 2015 Link to this post


    i create a new Instance from a Domain Class Object.
    I add this Object to the DataContext and when i discard the changes with the following code


    if (dbContext.HasChanges == true)
    { dbContext.ClearChanges(); }


    and create a new Instance with the same Guid (PrimaryKey) and Add to Context

    then i become the DuplicateKeyException Error Message
    'ZIS_ING_BAU.Database.ZIS_SCHAEDEN_CHECK' with identity '400587378-c50c1778-6de2-447f-a9d3-0e1f48a6cd4c' already exists in the cache of the object scope.

    I have no Second Level Cache in the BackendConfiguration and the Option CachePolicy is set to NoCache in the Domain Class.

    What's wrong ?
  2. Armin
    Armin avatar
    11 posts
    Member since:
    Feb 2011

    Posted 03 Feb 2015 in reply to Armin Link to this post

    Little Sample with another Domain Class from my Domain Model

                ZIS_ING_BAU.Database.ZIS_STANDORTE m_Standort = new Database.ZIS_STANDORTE();

                Guid m_Guid = Guid.NewGuid();

                m_Standort.PK_STANDORT = m_Guid;
                m_Standort.BEARB_DAT = DateTime.Now;
                m_Standort.BEARBEITER = "";
                m_Standort.NAME = "";



                if (((INavigation)this.Parent).DB_DataContext.dbContext.HasChanges == true)
                { ((INavigation)this.Parent).DB_DataContext.dbContext.ClearChanges(); }


                ObjectState m_State = ((INavigation)this.Parent).DB_DataContext.dbContext.GetState(m_Standort);
                ZIS_ING_BAU.Database.ZIS_STANDORTE m_Standort3 = new Database.ZIS_STANDORTE();

                m_Standort3.PK_STANDORT = m_Guid;
                m_Standort3.BEARB_DAT = DateTime.Now;
                m_Standort3.BEARBEITER = "";
                m_Standort3.NAME = "";


    -->   DuplicateKeyException
  3. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 06 Feb 2015 Link to this post

    Hi Armin,

    This is a by design behavior. Once you add an entity in the context it becomes tracked and if you try to add an entity with the same primary key, regardless if you have saved or cleared the changes you will get the mentioned exception. In order to solve this issue I can suggest you the following alternatives:

    - Instead of Add you can use AttachCopy which will check if the entity is already present in the context or in the DB before adding it. You should know that this approach will cause some additional selects for checking weather an entity with this Id is present in the DB.

    - You can use sort living context instances so that each add happens in separate context instance. 

    I hope this helps. Should you have any additional questions do not hesitate to get back to use. 

    Kaloyan Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
Back to Top