私はこれに対する答えをどこにも見つけることができませんでした、そして私は誰かが私を助けてくれることを望んでいます。私はWPFとEntityFrameworkの初心者なので、おそらくこれをすべて間違って行っています。
新しいレコードを作成し、それらをエンティティコレクションに追加している状況があります。ユーザーがやりたいことをすべて完了し、フォームを離れる前に最後の保存を実行できるようにするために、まだSaveChanges()を実行したくありません。私の問題は、レコードがEntityコレクションに追加された後、エンティティでSaveChanges()が実行されるまで、レコードを取得して表示することができないことです。
これは基本的に2つのDataGridを使用するマスター-子表示ですが、テーブルのフィールドの1つをよりユーザーフレンドリーな表示に変換する必要があるため、マスターグリッドソースは作成中のList<>です。そして、これはすべて1つのテーブルからのものであり、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)する前に、これらの新しく追加されたレコードを取得する方法はありますか?
ありがとう、マイク