实体框架5 - 读取记录然后在循环中删除它

时间:2013-01-24 18:38:37

标签: asp.net-mvc c#-4.0 entity-framework-5

我的申请存在问题。我有一个用于打印队列的db表。当我从循环中读取该表时,一旦我将该记录添加到视图模型中,我就想从数据库中删除它......这将是最有效的方法,但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();
        }

1 个答案:

答案 0 :(得分: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;
    }