实体框架:值不能为空。参数名称:实体

问题描述 投票:0回答:1

我有一个 WPF 应用程序,我在其中使用 SQL 模型。除了列表视图上的“删除”按钮外,我的应用程序运行良好。当我按下按钮“Вы уверены что хотите удалить катер”时,出现错误“值不能为空”。参数名称:实体。

请帮忙。(((

我的页面:

<Grid>
    <ListView SelectedItem="{Binding currentBoat}" ItemsSource="{Binding Boats}" Name="ListViewBoats" HorizontalAlignment="Center"
      Margin="0,50,0,125">
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Код_катера}"
            Header="Код катера" Width="100" />
                <GridViewColumn DisplayMemberBinding="{Binding Path=Название}"
            Header="Название" Width="100"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Тип}"
            Header="Тип" Width="150"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Водоизмещение}"
            Header="Водоизмещение" Width="150"/>
                <GridViewColumn DisplayMemberBinding="{Binding Path=Дата_постройки, StringFormat='yyyy-MM-dd'}"
            Header="Дата постройки" Width="150"/>
            </GridView>
        </ListView.View>
    </ListView>

    <Button Name="AddBtn" Content="Добавить" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="198,355,0,0" Click="AddBtn_Click"></Button>
    <Button Name="DeleteBtn" Content="Удалить" Click="DeleteBtn_Click" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="423,355,200,0"/>
</Grid>

我的删除按钮:

private void DeleteBtn_Click(object sender, RoutedEventArgs e)
{
    var currentBoat = (sender as Button).DataContext as Катера;
    if (MessageBox.Show("Вы точно хотите выполнить удаление?", "Внимание!",
        MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
    {
        try
        {
            DBGaneevaEntities.GetContext().Катера.Remove(currentBoat);
            DBGaneevaEntities.GetContext().SaveChanges();

            ListViewBoats.ItemsSource = DBGaneevaEntities.GetContext().Катера.ToList();
        }

        catch (Exception ex)
        {
            MessageBox.Show(ex.Message.ToString());
        }
    }
}
c# wpf listview
1个回答
0
投票

我猜您想删除

ListView
中当前选定的项目(而不是单击的
DataContext
Button
):

var currentBoat = ListViewBoats.SelectedItem as Катера;
© www.soinside.com 2019 - 2024. All rights reserved.