none
System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use RRS feed

  • Question

  • My function is used to update record in a table and it is working properly

    It generated the following error message few days ago suddenly

    System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use

    I found that only 1 record exists in the table with the key. I am sure that no changes in database schema.

    I have no any idea because my function only update record in database, no data insertion

    Monday, June 27, 2016 8:16 AM

All replies

  • Hi tonylck,

    Which kind of app did you create?

    >>System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key that is already in use.

    Like this thread here:

    http://forums.asp.net/t/1199308.aspx

    Could you provide us the main code which generated this exception in your side?

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, June 29, 2016 5:17 AM
  • public void CommitDC(bool run) { if (run) { try { dataContext.CommandTimeout = 3 * 60; dataContext.SubmitChanges(); } catch (Exception ex) { throw ex; } } }

    ITFPending inPending = dataContext.ITFPendings.Where(itf => (Guid)itf.gNistID == (Guid)pending.gNistID).OrderByDescending(itf => itf.dUpdateDate).FirstOrDefault();

                    if (!String.IsNullOrEmpty(pending.sDSFSMKey))
                        inPending.sDSFSMKey = pending.sDSFSMKey;
                    if (!String.IsNullOrEmpty(pending.sJobPackageId))
                        inPending.sJobPackageId = pending.sJobPackageId;
                    if (!String.IsNullOrEmpty(pending.sJobType))
                        inPending.sJobType = pending.sJobType;
                    if (!String.IsNullOrEmpty(pending.sResponseCode))
                        inPending.sResponseCode = pending.sResponseCode;
                    if (!String.IsNullOrEmpty(pending.sWorkstationId))
                        inPending.sWorkstationId = pending.sWorkstationId;
                    if (!String.IsNullOrEmpty(pending.sMode))
                        inPending.sMode = pending.sMode;
                    if (!String.IsNullOrEmpty(pending.sJobPackageId))
                        inPending.sJobPackageId = pending.sJobPackageId;
                    if (pending.gMatchKey != null)
                        inPending.gMatchKey = pending.gMatchKey;
                    inPending.dUpdateDate = DateTime.Now;
                    CommitDC(_commit);

    It only execute update query. I don't know why it generate the duplicated key error


    Wednesday, June 29, 2016 9:21 AM
  • Hi tonylck,

    Thanks for your friendly response.

    Since this issue is related to the Linq, I help you move this thread to the LINQ to SQL forum, there you would get dedicated support.

    Best Regards,

    Jack


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, June 30, 2016 5:18 AM
  • Hi tonylck,

    According to your code snippet, I create a similar demo as below, it works fine.

    using (var db = new DataClasses1DataContext())
                {
                    var item = db.products
                        .Where(c => c.ProductName.Contains("B"))
                        .OrderByDescending(x => x.UpdateTime)
                        .FirstOrDefault();
    
                    item.ProductName = "Test";
    
                    db.SubmitChanges();
                    Console.WriteLine("{0} -- {1} -- {2} -- {3}", item.ProductId, item.ProductName, item.Stutas,item.UpdateTime);
                    Console.ReadKey();
                }

    In addition, about the error message, it seems that your datacontext (db) cannot track the same entity more than once. the following link provide a solution with refresh method for your reference.

    public void Update(Customer customer)
    {
      NorthwindDataContext context = new NorthwindDataContext();
      context.Attach(customer);
      context.Refresh(RefreshMode.KeepCurrentValues, customer);
      context.SubmitChanges();
    }

    http://stackoverflow.com/questions/1758214/linq-to-sql-exception-with-attach-cannot-add-an-entity-with-a-key-that-is-alr

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 7, 2016 2:10 AM
    Moderator
  • Hi Cole,

    I never use Attach method in my submitted sample code. The sample code is working properly for about 1 year. It sometimes generates the linq error recently. I have no any idea to find out the main cause of the problem

    Tony

    Monday, August 8, 2016 8:30 AM