简体   繁体   English

更新实体框架导航属性ICollection项

[英]Update Entity Framework navigation property ICollection items

I'm using Entity Framework 6 in Visual Studio 2015 to build an MVVM Light WPF app. 我正在Visual Studio 2015中使用Entity Framework 6来构建MVVM Light WPF应用程序。 I need to bind a navigation property ICollection to some CheckBox controls. 我需要将导航属性ICollection绑定到某些CheckBox控件。 An Employee can have 0 to at most 3 EmployeeStatus entities, with employeeID serving as the key on the EmployeeStatus entity; 一个Employee最多可以有0到3个EmployeeStatus实体,而employeeID作为EmployeeStatus实体上的键; EmployeeStatus in turn has a foreign key on the EmployeeStatusDescription table of employeeStatusID. EmployeeStatus依次在employeeStatusID的EmployeeStatusDescription表上具有外键。 The EmployeeStatusDescription provides the description of the status code (such as "Archived", "Inactive", "Leave of Absence"). EmployeeStatusDescription提供状态代码的描述(例如“已归档”,“无效”,“缺勤”)。 Each EmployeeStatus corresponds to one EmployeeStatusDescription. 每个EmployeeStatus对应一个EmployeeStatusDescription。

EmployeeStatus is defined this way in the Employee class: EmployeeStatus是在Employee类中定义的:

public virtual ICollection<EmployeeStatu> EmployeeStatus { get; set; }

EmployeeStatusDescription is defined in the EmployeeStatus class as: EmployeeStatusDescription在EmployeeStatus类中定义为:

public virtual EmployeeStatusDescription EmployeeStatusDescription { get; set; }

I'd like to show 3 CheckBox controls and bind each to the value from the EmployeeStatus ICollection values. 我想显示3个CheckBox控件,并将每个控件绑定到EmployeeStatus ICollection值中的值。 For example, if an employee does not have status "Inactive" and the user checks that, I need to add that to the EmployeeStatus collection; 例如,如果某个员工的状态不为“非活动”,并且用户检查了该状态,则需要将其添加到EmployeeStatus集合中; if the user unchecks that item, I'd like to have it removed from the EmployeeStatus collection. 如果用户取消选中该项目,则希望将其从EmployeeStatus集合中删除。

I've created the following StackPanel to hold the checkboxes; 我创建了以下StackPanel来容纳复选框; they're bound to properties on my view model: 它们绑定到我的视图模型上的属性:

<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>

Here's an example property bound to one of the CheckBox controls' IsChecked dependency property: 这是绑定到CheckBox控件的IsChecked依赖项属性之一的示例属性:

private bool _isSelectedEmployeeInActive;

public bool IsSelectedEmployeeInActive
{
    get { return _isSelectedEmployeeInActive; }
    set
    {
        if (_isSelectedEmployeeInActive == value) return;

        _isSelectedEmployeeInActive = value;
        RaisePropertyChanged(() => IsSelectedEmployeeInActive);
    }
}   

I'm doing a search to get the entity collection: 我正在搜索以获取实体集合:

var query = (from e in Context.Employees
             .Include("EmployeeStatus.EmployeeStatusDescription")
             .Where(comparison)
             select e);

SearchResults = new ObservableCollection<Employee>(query);

Inside of your properties I would also add/remove from the collection. 在您的属性内,我还将添加/从集合中删除。 If you don't already, store the selected employee somewhere so that you can access it. 如果还没有,请将所选员工存储在某个地方,以便您可以访问它。

           if (_isSelectedEmployeeInActive == value) return;

            _isSelectedEmployeeInActive = value;

            //do updates to collection here
            if (value)
            {
                SelectedEmployee.EmployeeStatus.Add(new EmployeeStatus("Inactive"));
            }
            else
            {
                SelectedEmployee.EmployeeStatus.Remove("Inactive"));
            }

            RaisePropertyChanged(() => IsSelectedEmployeeInActive);

I'm not sure how your employee status looks but you get the idea. 我不确定您的员工状况如何,但是您知道了。 Just make sure when you're done checking the boxes you call SaveChanges on your context so that your changes are saved to the database. 只需确保完成后选中在上下文中调用SaveChanges的框,即可将所做的更改保存到数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM