【问题标题】:Retrieving entity records before savingchanges在保存更改之前检索实体记录
【发布时间】:2023-03-03 10:21:01
【问题描述】:

我只是无法在任何地方找到答案,我希望有人可以帮助我。我是 WPF 和实体框架的新手,所以可能做错了。

我遇到了一种情况,我正在创建新记录并将它们添加到实体集合中。我不想对其执行 SaveChanges() 以允许用户完成他们想做的所有事情,然后在离开表单之前进行最终保存。我的问题是,在将记录添加到实体集合后,我无法将它们检索出来以显示“直到”它们在实体上运行了 SaveChanges()。

这基本上是一个使用两个 DataGrid 的主子显示,但主网格源是一个正在构建的 List,因为需要将表中的一个字段转换为对用户更友好的显示。这一切都来自一个表,我只是根据其中的 3 个字段(作为主)对数据进行分组,然后将子网格显示为该分组的详细信息。

此操作是一个“复制”操作,我在其中获取现有 DataGrid 数据显示并让用户将数据复制到“新”主组。我正在使用以下内容来创建这个新的记录组。

            RateGroupRow rgr = new RateGroupRow();
            rgr.StaffRole = rg.SelectedStaffRole;
            rgr.Department = rg.SelectedDepartment;
            rgr.PlanYear = rg.SelectedPlanYear;
            _rateGroupQuery.Add(rgr);

            foreach (var item in dgRateValues.Items)
            {
                if (item.GetType() == typeof(CommissionRate))
                {
                    CommissionRate cr = (CommissionRate)item;
                    CommissionRate newcr = new CommissionRate();
                    newcr.StaffRole = Common.StaffTextToStaffType(rgr.StaffRole);
                    newcr.Department = rgr.Department;
                    newcr.PlanYear = rgr.PlanYear;
                    newcr.Low = cr.Low;
                    newcr.High = cr.High;
                    newcr.Rate = cr.Rate;
                    Common.CommissionsEntities.CommissionRates.AddObject(newcr);
                }
            }
            //TODO:  Don't want to do this SaveChanges here but for now that's the only way I can get these new records to be displayed in the data selection.
            //Common.CommissionsEntities.SaveChanges();

            dgRateGroups.ItemsSource = null;
            dgRateGroups.ItemsSource = _rateGroupQuery;
            dgRateGroups.SelectedIndex = dgRateGroups.Items.Count - 2;

将主记录添加到 _rateGroupQuery 后,DataGrid (dgRateGroups) 会选择 SelectionChanged 查询数据的行,以将详细信息 DataGrid 填充为:

    dgRateValues.ItemsSource = null;
    _CurrentSelectedStaffRole = rateGroupRow.StaffRole;
    _CurrentSelectedDepartment = rateGroupRow.Department;
    _CurrentSelectedPlanYear = rateGroupRow.PlanYear;
    string StaffNameType = Common.StaffTextToStaffType(_CurrentSelectedStaffRole);

    var CommissionRatesItems =
        from cr in Common.CommissionsEntities.CommissionRates
        where cr.StaffRole == StaffNameType &&
              cr.Department == _CurrentSelectedDepartment &&
              cr.PlanYear == _CurrentSelectedPlanYear
        orderby cr.Low
        select cr;
    ObjectQuery<CommissionRate> CommissionRatesQuery = CommissionRatesItems as ObjectQuery<CommissionRate>;
    dgRateValues.ItemsSource = CommissionRatesQuery.Execute(MergeOption.AppendOnly);

我没有从这个选择中得到任何记录。如果我在添加记录后调用“SaveChanges()”,那么一切正常。

那么有没有办法在这些新添加的记录被提交(SaveChanges)回数据库之前检索它们?

谢谢, 迈克

【问题讨论】:

    标签: wpf linq entity-framework


    【解决方案1】:

    您可以取回它们,但不能在 ObjectQuery 中取回。相反,您必须通过 ObjectStateManager 并找到已添加的实体,然后从中构建您的绑定列表。这很痛苦,而且效果不佳。

    幸运的是,如果您使用 EF 4.1 及更高版本附带的 DbContext,那么数据绑定体验会好得多。 DbContext 为每种类型的实体提供了一个 ObservableCollection,作为实体 DbSet 上的 .Local 属性。该集合包含所有与数据绑定相关的事件,并自动包括新添加的实体,同时也排除标记为删除的实体。更多信息和演练可以在这里找到:http://msdn.microsoft.com/en-us/data/jj574514.aspx

    【讨论】:

    • 非常感谢亚瑟,我会查看链接。我相信我只是在使用 EF 4.0,因为我是在 Framework 4.0 上构建的,但我会研究一下。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多