0

アプリケーションに問題があります。印刷キュー用のdbテーブルがあります。そのテーブルからループで読み取るとき、そのレコードをビューモデルに追加したら、データベースから削除したいと思います...これが最も効率的な方法ですが、EFは次のように吠えます。

エンティティオブジェクトは、IEntityChangeTrackerの複数のインスタンスから参照することはできません。

複数のコンテキストを使用してみましたが、それもうまくいかなかったようです。Rick Strahlのような記事を見たことがありますが、率直に言って、それは私の理解レベルを超えており、それがここでの私の問題に役立つかどうかは正確にはわかりません。

ここで達成しようとしていることを達成する簡単な方法はありますか?

これが私のコードです:

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 に答える