Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:04:36+00:00 2026-06-17T21:04:36+00:00

I am having issues with my application. I have a db table for a

  • 0

I am having issues with my application. I have a db table for a print queue. When I read from that table in a loop, once I add that record to the view model, I then want to delete it from the database…this would be the most efficient way to do it, but EF barks:

An entity object cannot be referenced by multiple instances of IEntityChangeTracker.

I’ve tried using multiple contexts… but that didn’t seem to work either. I’ve seen articles like Rick Strahl’s, but frankly it was above my level of understanding, and not exactly sure if it helps my issue here and seemed quite an in depth solution for something as simple as this.

Is there a simple way to accomplish what I am trying to achieve here?

Here is my code:

public List<InventoryContainerLabelViewModel> CreateLabelsViewModel(int intFacilityId)
        {
            var printqRep = new Repository<InventoryContainerPrintQueue>(new InventoryMgmtContext());
            var printqRepDelete = new Repository<InventoryContainerPrintQueue>(new InventoryMgmtContext());
            IQueryable<InventoryContainerPrintQueue> labels = 
                printqRep.SearchFor(x => x.FacilityId == intFacilityId);

            List<InventoryContainerLabelViewModel> labelsViewModel = new List<InventoryContainerLabelViewModel>();

            if (labels.Count() > 0)
            {
                //Get printq record
                foreach (InventoryContainerPrintQueue label in labels)
                {
                    IEnumerable<InventoryContainerDetail> icDtls = 
                        label.InventoryContainerHeader.InventoryContainerDetails;

                    //Get print details
                    foreach (InventoryContainerDetail icDtl in icDtls)
                    {
                        labelsViewModel.Add(new InventoryContainerLabelViewModel()
                            {
                                ...
                                populate view model here
                            }
                        );//Add label to view model

                    } //for each IC detail

                    //Delete the printq record
                    printqRepDelete.Delete(label); <======== Error Here

                } //foreach label loop
            }//label count > 0
            return labelsViewModel.ToList();
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:04:37+00:00Added an answer on June 17, 2026 at 9:04 pm

    In the end, I added a column to the printq table for status, then in the the loop updated it to processed, then called a separate method to delete it.

    public List<InventoryContainerLabelViewModel> CreateLabelsViewModel(int intFacilityId)
        {
            InventoryMgmtContext dbContext = new InventoryMgmtContext();
            var printqRep = new Repository<InventoryContainerPrintQueue>(dbContext);
    
            IEnumerable<InventoryContainerPrintQueue> unprintedPrtqRecs = 
                printqRep.SearchFor(x => x.FacilityId == intFacilityId && x.Printed == false);
    
            List<InventoryContainerLabelViewModel> labelsViewModel = new List<InventoryContainerLabelViewModel>();
    
            if (unprintedPrtqRecs.Count() > 0)
            {
                //Get printq record
                foreach (InventoryContainerPrintQueue unprintedPrtqRec in unprintedPrtqRecs)
                {
                    IEnumerable<InventoryContainerDetail> icDtls = 
                        unprintedPrtqRec.InventoryContainerHeader.InventoryContainerDetails;
    
                    //Get container details to print
                    foreach (InventoryContainerDetail icDtl in icDtls)
                    {
                        labelsViewModel.Add(new InventoryContainerLabelViewModel()
                            {
                                ...
                            }
                        );//Get IC details and create view model
                    } //for each IC detail
    
                    unprintedPrtqRec.Printed = true;
                    printqRep.Update(unprintedPrtqRec, unprintedPrtqRec, false);
    
                } //foreach label loop
    
                //Commit updated to Printed status to db
                dbContext.SaveChanges();
    
            }//label count > 0
            return labelsViewModel;
        }
    
        public ActionConfirmation<int> DeletePrintQRecs(int intFacilityId)
        {
            InventoryMgmtContext dbContext = new InventoryMgmtContext();
            var printqRep = new Repository<InventoryContainerPrintQueue>(dbContext);
    
            IEnumerable<InventoryContainerPrintQueue> printedPrtqRecs =
                printqRep.SearchFor(x => x.FacilityId == intFacilityId && x.Printed == true);
    
            foreach (InventoryContainerPrintQueue printedPrtqRec in printedPrtqRecs)
            {
                //Delete the printq record
                printqRep.Delete(printedPrtqRec, false);
            }
    
            //Save Changes on all deletes
            ActionConfirmation<int> result;
            try
            {
                dbContext.SaveChanges();
                result = ActionConfirmation<int>.CreateSuccessConfirmation(
                            "All Label Print Q records deleted successfully.",
                            1);
            }
            catch (Exception ex)
            {
                result = ActionConfirmation<int>.CreateFailureConfirmation(
                    string.Format("An error occured attempting to {0}. The error was: {2}.",
                            "delete Label Print Q records",
                            ex.Message),
                            1
                            );
            }
    
            return result;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that reads records from the database and exports it out
I'm having issues with my MVC3 vb.net application. When I try to post the
I am having issues with paperclip in my rails application. I am able to
I am having some issues with deploying my MVC 2 application on a IIS
I ran dotTrace on my application (which is having some issues). IntPtr System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr,
I'm having an issue with pulling a Spring bean from an application context. When
Having issues referencing $(this) from within a the nested ajax 'success' function... I know
I have a windows service that listens to a message queue and processes request
I've read in forums about people having issues with certain data types when accessing
I have a table in SQL Server 2008 with a column having the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.