This is a migrated thread and some comments may be shown as answers.

DuplicateKeyException

2 Answers 157 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Armin
Top achievements
Rank 1
Armin asked on 03 Feb 2015, 10:18 AM
Hello,

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

dbContext.FlushChanges();

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

dbContext.SaveChanges();

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 Answers, 1 is accepted

Sort by
0
Armin
Top achievements
Rank 1
answered on 03 Feb 2015, 01:38 PM
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 = "";

            ((INavigation)this.Parent).DB_DataContext.Add2Context(m_Standort);

            ((INavigation)this.Parent).DB_DataContext.dbContext.FlushChanges();

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

            ((INavigation)this.Parent).DB_DataContext.dbContext.SaveChanges();

            
            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 = "";

            ((INavigation)this.Parent).DB_DataContext.Add2Context(m_Standort3);

-->   DuplicateKeyException
0
Kaloyan Nikolov
Telerik team
answered on 06 Feb 2015, 09:43 AM
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. 


Regards,
Kaloyan Nikolov
Telerik
 
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
 
Tags
General Discussions
Asked by
Armin
Top achievements
Rank 1
Answers by
Armin
Top achievements
Rank 1
Kaloyan Nikolov
Telerik team
Share this question
or