MVVM Light WPFアプリケーションをビルドするために、Visual Studio 2015でEntity Framework 6を使用しています。ナビゲーションプロパティICollectionをいくつかのCheckBoxコントロールにバインドする必要があります。従業員は0〜最大で3つのEmployeeStatusエンティティを持つことができます。従業員IDはEmployeeStatusエンティティのキーとして機能します。 EmployeeStatusは次に、employeeStatusIDのEmployeeStatusDescriptionテーブルに外部キーを持ちます。 EmployeeStatusDescriptionはステータスコードの説明を提供します(例えば、 "Archived"、 "Inactive"、 "Leave of Absence")。各EmployeeStatusは1つのEmployeeStatusDescriptionに対応します。Entity Frameworkのナビゲーションのプロパティを更新するICollectionの項目
EmployeeStatusは、Employeeクラスではこのように定義されて:としてEmployeeStatusクラスで定義されている
public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }
EmployeeStatusDescription:私は3つのCheckBoxコントロールを表示し、各バインドしたい
public virtual EmployeeStatusDescription EmployeeStatusDescription { get; set; }
EmployeeStatus ICollection値からの値。たとえば、従業員がステータスが「非アクティブ」でなく、ユーザーがそれを確認した場合、その従業員をEmployeeStatusコレクションに追加する必要があります。ユーザーがその項目のチェックを外した場合は、EmployeeStatusコレクションから削除するようにしたいと思います。
私はチェックボックスを保持するために次のStackPanelを作成しました。彼らは私のビューモデルのプロパティにバインドされている:
<StackPanel Grid.Row="12"
Grid.Column="1"
Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="Inactive" />
<CheckBox IsChecked="{Binding IsSelectedEmployeeInActive}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="Leave of Absence" />
<CheckBox IsChecked="{Binding IsSelectedEmployeeLoa}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock HorizontalAlignment="Left"
VerticalAlignment="Top"
Text="Archived" />
<CheckBox IsChecked="{Binding IsSelectedEmployeeArchived}" />
</StackPanel>
</StackPanel>
ここでチェックボックスのいずれかに結合した実施例のプロパティがIsChecked
依存関係プロパティを制御しています:
private bool _isSelectedEmployeeInActive;
public bool IsSelectedEmployeeInActive
{
get { return _isSelectedEmployeeInActive; }
set
{
if (_isSelectedEmployeeInActive == value) return;
_isSelectedEmployeeInActive = value;
RaisePropertyChanged(() => IsSelectedEmployeeInActive);
}
}
私が取得するために検索をやっていますエンティティコレクション:
var query = (from e in Context.Employees
.Include("EmployeeStatus.EmployeeStatusDescription")
.Where(comparison)
select e);
SearchResults = new ObservableCollection<Employee>(query);
ありがとう、@ bme2010。すでに存在していないEmployeeStatusesのICollectionに値を追加しないようにするにはどうすればよいでしょうか?その値がすでに存在する場合、その '.Add()'にEFが吹き飛ばされますか? – Alex
EFは、外部キー制約に違反する場合にのみ爆発します。同じ名前の複数の値を持つことができる場合は、それらを追加することができます。重複をチェックするには、LINQ '.Where(x => x.Name ==" Inactive ")を使用することができます。ICollectionのCount()' – Brandon
もう一度@ bme2010に感謝します。 'Add()'を実行する前にチェックを行います。 – Alex