none
Entity Framework SaveChanges Error RRS feed

  • Question

  • So I am getting this strange error with my entity framework saving. The following code is for adding elements to to my Elements Table and if they exist it simply retrieves those objects. Then it Creates new ElementVerison objects and adds the correct field information and pairs it with the Element object (whether it was new or existed).

    //Creates the files and Element Versions that will be paired with RoundReviewerStatuses
                for (int i = 0; i < review.ReviewFiles.Count; i++)
                {
                    string fileName = review.ReviewFiles[i].Name;
                    string fileType = review.ReviewFiles[i].FileType;
                    
                    //Grabs the Element from the database (or sets to null)
                    Model.PeerReviewDatabase.Element dbElement = context.Elements.Where(a =>
                        a.Name.Equals(fileName) &&
                        a.FileType.Equals(fileType)).FirstOrDefault();
                    //Checks to see if the element existed in the database and 
                    //if not creates a new one to populate the element data with
                    if (dbElement == null)
                    {
                        dbElement = context.Elements.Create();
                        
                        //Populates Element Data
                        dbElement.FileType = fileType;
                        dbElement.Name = fileName;
                    }
                    //Model.PeerReviewDatabase.ElementVersion dbOrigVersion = context.ElementVersions.Create();
                    Model.PeerReviewDatabase.ElementVersion dbVersion = context.ElementVersions.Create();
                    //Populates First(Version 1) ElementVersion Data
                    dbVersion.Round = dbRound;
                    dbVersion.Element = dbElement;
                    //Fills in the Version info
                    if (review.ID == -1)
                    {
                        dbVersion.Version = 1;
                    }
                    else
                    {
                        dbVersion.Version = (short)(GetElementVersions(fileName, 
                            review.ReviewFiles[i].Path, review.ID).Count() + 1);
                    }
                    dbVersion.Path = review.ReviewFiles[i].Path;
                    dbVersion.FileContent = review.ReviewFiles[i].FileContent;
                    //Grabs handle to the version and adds to the context to be saved
                    versionList.Add(dbVersion);
                    context.ElementVersions.Add(dbVersion);
                }

    Finally at the end of the code i say:

    context.SaveChanges();

    At this point I recieve an error saying

    "Cannot insert duplicate key row in object 'dbo.ElementVersions' with unique index 'IX_ElementVersions'.
    The statement has been terminated."

    I have searched debugged and everything seems right, I just cant figure out why it thinks there is a duplicate key being generated. Please help!

    Wednesday, September 5, 2012 4:58 PM

Answers

  • Alright I finally solved this, the checks for uniqueness on the identity column were making it seem like multiple rows were being duplicated in the database so I had to add an additional check so that the uniqueness property would be strictly based on the elementID, Version and RoundID combination.
    • Marked as answer by NerdHerd91 Thursday, September 6, 2012 2:19 PM
    Thursday, September 6, 2012 2:19 PM

All replies

  • Alright I finally solved this, the checks for uniqueness on the identity column were making it seem like multiple rows were being duplicated in the database so I had to add an additional check so that the uniqueness property would be strictly based on the elementID, Version and RoundID combination.
    • Marked as answer by NerdHerd91 Thursday, September 6, 2012 2:19 PM
    Thursday, September 6, 2012 2:19 PM
  • Good job , NerdHerd91 !

    Thanks for your sharing. This is helpful for other developers in the forum who have interests in this problem.

    Have a nice day!

    Regards ,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us

    Friday, September 7, 2012 5:33 AM
    Moderator