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.InternalSet
1.<>c__DisplayClassa.<Attach>b__9() at System.Data.Entity.Internal.Linq.InternalSet
1.ActOnSet(Action action, EntityState newState, Object entity, String methodName) at System.Data.Entity.Internal.Linq.InternalSet1.Attach(Object entity) at System.Data.Entity.DbSet
1.Attach(TEntity entity) at DT.ValidatorCore.JobQueue.MasTJobQueueMySql.<>c.b__23_7(jobqueue jq) at System.Collections.Generic.List1.ForEach(Action
1 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.BaseCommand
1.TriggerSubCommand(CmdTrigger1 trigger) at DT.Common.Commands.Command
1.Process(CmdTrigger1 trigger) at DT.Common.Commands.CommandMgr
1.Execute(CmdTrigger1 trigger, BaseCommand
1 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!