Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 在保存更改之前检索实体记录_Wpf_Linq_Entity Framework - Fatal编程技术网

Wpf 在保存更改之前检索实体记录

Wpf 在保存更改之前检索实体记录,wpf,linq,entity-framework,Wpf,Linq,Entity Framework,我只是在任何地方都找不到这个问题的答案,我希望有人能帮助我。我是WPF和实体框架的新手,所以可能做得都不对 我遇到了这样一种情况:创建新记录并将其添加到实体集合中。我不想在表单上执行SaveChanges(),以允许用户完成所有他们想做的事情,然后在离开表单之前进行最后一次保存。我的问题是,在将记录添加到实体集合后,我无法将其检索出来以显示“直到”它们在实体上运行了SaveChanges() 这基本上是一个主-子显示,使用两个数据网格,但主网格源是一个正在构建的列表,因为表中的一个字段需要转换为

我只是在任何地方都找不到这个问题的答案,我希望有人能帮助我。我是WPF和实体框架的新手,所以可能做得都不对

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

这基本上是一个主-子显示,使用两个数据网格,但主网格源是一个正在构建的列表,因为表中的一个字段需要转换为更用户友好的显示。所有这些都来自一个表,我只是根据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);
dgRateValues.ItemsSource=null;
_CurrentSelectedStaffRole=rateGroupRow.StaffRole;
_CurrentSelectedDepartment=rateGroupRow.Department;
_CurrentSelectedPlanYear=rateGroupRow.PlanYear;
字符串StaffNameType=Common.StaffTextToStaffType(\u CurrentSelectedStaffRole);
var佣金率=
来自cr的Common.CommissionEntities.CommissionRates
其中cr.StaffRole==StaffNameType&&
cr.部门==\u当前选定的部门&&
cr.PlanYear==\u当前选择的计划年
订购者cr.低
选择cr;
ObjectQuery CommissionRatesQuery=CommissionRateSites作为ObjectQuery;
dgRateValues.ItemsSource=CommissionRatesQuery.Execute(MergeOption.AppendOnly);
我没有从这个选择中得到任何记录。如果在添加记录后执行“SaveChanges()”调用,则一切正常

那么,在将这些新添加的记录提交(保存更改)回数据库之前,有没有办法检索这些记录呢

谢谢,,
Mike

您可以将它们取回,但不能在ObjectQuery中取回。相反,您必须遍历ObjectStateManager并找到添加的实体,然后从中构建绑定列表。这很痛苦,效果不太好


幸运的是,如果您使用EF4.1及更高版本附带的DbContext,那么数据绑定体验会更好。DbContext为每种类型的实体提供了一个ObservableCollection,作为该实体的DbSet上的.Local属性。此集合已将所有事件连接到数据绑定,并自动包括新添加的实体,同时也排除标记为删除的实体。更多信息和演练可在此处找到:

非常感谢Arthur,我将查看链接。我相信我只是在使用EF4.0,因为我是在Framework4.0上构建的,但我将对此进行研究。