1
votes

I have inherited some rather large code after a fellow employee left the company. Unfortunately the program broke the day after he left. Could anyone point me where to look with the following error?

Attaching an entity of type 'MasT.DB.jobqueue' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate. at

System.Data.Entity.Core.Objects.ObjectContext.VerifyRootForAdd(Boolean doAttach, String entitySetName, IEntityWrapper wrappedEntity, EntityEntry existingEntry, EntitySet& entitySet, Boolean& isNoOperation) at System.Data.Entity.Core.Objects.ObjectContext.AttachTo(String entitySetName, Object entity) at System.Data.Entity.Internal.Linq.InternalSet1.<>c__DisplayClassa.<Attach>b__9() at System.Data.Entity.Internal.Linq.InternalSet1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet1.Attach(Object entity) at System.Data.Entity.DbSet1.Attach(TEntity entity) at DT.ValidatorCore.JobQueue.MasTJobQueueMySql.<>c.b__23_7(jobqueue jq) at System.Collections.Generic.List1.ForEach(Action1 action) at DT.ValidatorCore.JobQueue.MasTJobQueueMySql.MaintainJobQueue() at DT.ValidatorCore.JobQueue.MasTJobQueueMySql.TakeJobsQueue(Boolean includeCompleted) at DT.ValidatorCore.JobQueue.MasTJobQueue.DoWork(String date, Boolean testRun, Int32 runId) at DT.ValidatorCore.Commands.TriggerCommand.QueueCommand.Process(CmdTrigger1 trigger) at DT.Common.Commands.BaseCommand1.TriggerSubCommand(CmdTrigger1 trigger) at DT.Common.Commands.Command1.Process(CmdTrigger1 trigger) at DT.Common.Commands.CommandMgr1.Execute(CmdTrigger1 trigger, BaseCommand1 cmd, Boolean silentFail)

I am very confused as this does not happen when running in debug, only when the program is running on the production server. While they connect to two separate databases, these are identical.

Initially I was only asked to update certain parts of the code so this is a big jump!

I am fairly certain the issue is with the DT.ValidatorCore.JobQueue.MasTJobQueueMySql.MaintainJobQueue, but I have very limited knowledge of entity framework

    protected void MaintainJobQueue()
    {
        if (_jobQueueUnitOfWork != null)
            _jobQueueUnitOfWork.Dispose();

        _jobQueueUnitOfWork = new JobQueueUnitOfWork();

        List<jobqueue> tempList = _jobQueueUnitOfWork.JobQueueRepository.GetAll();

        if (tempList == null)
            return;

        tempList.RemoveAll(jqItem => jqItem == null);
        tempList.RemoveAll(jqItem => jqItem.packageinfo == null);
        tempList.RemoveAll(jqItem => jqItem.packageinfo.pkg_content_id == null);

        if (!tempList.Any())
            return;

        var tempList2 = tempList.GroupBy(g => g.packageinfo.pkg_content_id + g.packageinfo.pkg_master_version + g.packageinfo.app_version).Select(x => x.ToList().OrderByDescending(m => m.packageinfo.app_revision).First()).ToList();

        tempList.RemoveAll(i => tempList2.Contains(i));
        tempList.ForEach(jq => context.jobqueue.Attach(jq));


        var pkgInfoRemovals = tempList.Select(i => i.packageinfo);
        _jobQueueUnitOfWork.PackageInfoRepository.DeleteRange(pkgInfoRemovals);

        var submissionpathRemovals = tempList.Select(i => i.submissionpath);
        context.submissionpath.RemoveRange(submissionpathRemovals);
        _jobQueueUnitOfWork.SubmissionPathRepository.DeleteRange(submissionpathRemovals);


        _jobQueueUnitOfWork.JobQueueRepository.DeleteRange(tempList);
    }

    protected override void SaveChanges()
    {
        _jobQueueUnitOfWork.Save();
    }

Cheers!

1
It's hard to tell by only seeing the exception and no code, but it's as it says, your table has a unique key constraint where every value must be unique and you're trying to add a value with a duplicate key. Can you confirm if the row(s) being added are new or modified?Equalsk
The rows are added initially at the start of each day & then updated once later on. I created a blank database and it will work for one day correctly & then error out the following morningOrangeFlavour
So...which bit does it fail on, the adding or the updating?Equalsk
Sorry, I updated the comment as you responded - "I created a blank database and it will work for one day correctly & then error out the following morning"OrangeFlavour
That means nothing to me, I don't know what it does one day from the next. I assume it fails when the rows are updated? How are you retrieving and then modifying the rows in the database? It sounds like they become detached and so EF tries to insert rather than update. It's hard to guess unless you show the code that throws the exception.Equalsk

1 Answers

1
votes

It's hard to be sure as the code you've shared appears to be a wrapper built around Entity Framework and so obscures away some of the necessary detail but an educated guess says that you're dealing with Detached Entities.

The keyword to search for is DbContext (Database Context).

If you use Entity Framework (EF) to fetch some data from your database this data remains attached to the database or DbContext, this is an attached entity.
Any changes made to the data are now automatically tracked by EF so when you call SaveChanges() it knows to UPDATE the existing data.

In your case I suspect that _jobQueueUnitOfWork.JobQueueRepository.GetAll(); fetches data from somewhere else such as a Web API. As this data was created outside of DbContext then EF can't possibly know what state it's in, this is a detached entity.
The solution is to simply tell EF what state the data is in, in your case it's modified and requires an UPDATE over an INSERT.

tempList.ForEach(jq => 
{ 
    context.jobqueue.Attach(jq); // Attach to `DbContext`
    context.Entry(jq).State = System.Data.Entity.EntityState.Modified; // Modified
});

If you search for Entity Framework articles relating to dbcontext, change tracking and attached/detached entities it should answer a lot of your questions.