Reputation: 81
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!
Upvotes: 3
Views: 5332
Reputation: 8214
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.
Upvotes: 3