0

我的申请有问题。我有一个用于打印队列的数据库表。当我在循环中从该表中读取时,一旦将该记录添加到视图模型中,然后我想将其从数据库中删除......这将是最有效的方法,但是 EF 咆哮:

一个实体对象不能被多个 IEntityChangeTracker 实例引用。

我尝试过使用多个上下文......但这似乎也不起作用。我看过像Rick Strahl's这样的文章,但坦率地说,这超出了我的理解水平,并且不确定它是否对我的问题有帮助,并且对于像这样简单的事情似乎是一个相当深入的解决方案。

有没有一种简单的方法来完成我在这里想要实现的目标?

这是我的代码:

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();
        }
4

1 回答 1

0

最后,我在 printq 表中添加了一列用于状态,然后在循环中将其更新为已处理,然后调用单独的方法将其删除。

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;
    }
于 2013-01-25T21:38:09.693 回答