0

我正在使用以下内容:

  • 视觉工作室 2010,
  • .NET Framework 4 客户端配置文件,
  • WPF 应用程序,
  • 实体框架 6.0,
  • EntityFramework.SqlServer 6.0,
  • 使用 Entity Framework Power Tools Beta 3 逆向工程代码优先,
  • 具有表之间关系的实体-属性-值 SQL Server 数据库,
  • 数据源设备MasterDataSet绑定attributeDataGrid和entityDataGrid

MainWindow.xaml.cs如下:

using System.Data.Entity;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using EquipmentMaster.Models;

namespace EquipmentMaster
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public EquipmentMasterContext EquipmentMasterContext = new EquipmentMasterContext();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            CollectionViewSource equipmentMasterDataSet = ((CollectionViewSource)(this.FindResource("equipmentMasterDataSet")));

            // Load is an extension method on IQueryable,
            // defined in the System.Data.Entity namespace.
            // This method enumerates the results of the query,
            // similar to ToList but without creating a list.
            // When used with Linq to Entities this method
            // creates entity objects and adds them to the context.

            EquipmentMasterContext.Attributes.Load();

            // After the data is loaded call the DbSet<T>.Local property
            // to use the DbSet<T> as a binding source.
            equipmentMasterDataSet.Source = EquipmentMasterContext.Attributes.Local;
        }

        private void buttonSave_Click(object sender, RoutedEventArgs e)
        {
            // When you delete an object from the related entities collection 
            // (in this case Products), the Entity Framework doesn’t mark 
            // these child entities as deleted.
            // Instead, it removes the relationship between the parent and the child
            // by setting the parent reference to null.
            // So we manually have to delete the products 
            // that have a Category reference set to null.

            // The following code uses LINQ to Objects 
            // against the Local collection of Products.
            // The ToList call is required because otherwise the collection will be modified
            // by the Remove call while it is being enumerated.
            // In most other situations you can use LINQ to Objects directly 
            // against the Local property without using ToList first.
            foreach (var entity in EquipmentMasterContext.Entities.ToList())
            {
                EquipmentMasterContext.Entities.Remove(entity);
            }

            EquipmentMasterContext.SaveChanges();

            // Refresh the grid so the database generated values show up.
            this.attributeDataGrid.Items.Refresh();
            this.entityDataGrid.Items.Refresh();
        }

        protected override void OnClosed(System.EventArgs e)
        {
            base.OnClosed(e);

            this.EquipmentMasterContext.Dispose();
        }
    }
}

MainWindow.xaml

<Window x:Class="EquipmentMaster.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:my="clr-namespace:EquipmentMaster"
        Title="Equipment Master" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <my:EquipmentMasterDataSet x:Key="equipmentMasterDataSet" />
        <CollectionViewSource x:Key="entityViewSource" Source="{Binding Path=Entity, Source={StaticResource equipmentMasterDataSet}}" />
        <CollectionViewSource x:Key="attributeViewSource" Source="{Binding Path=Attribute, Source={StaticResource equipmentMasterDataSet}}" />
    </Window.Resources>
    <Grid DataContext="{StaticResource entityViewSource}">
        <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="137" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="12,127,0,0" Name="entityDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="479">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="iDColumn" Binding="{Binding Path=ID}" Header="ID" IsReadOnly="True" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="quantityColumn" Binding="{Binding Path=Quantity}" Header="Quantity" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="descriptionColumn" Binding="{Binding Path=Description}" Header="Description" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="fkParentColumn" Binding="{Binding Path=FkParent}" Header="Fk Parent" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="fkAttributeColumn" Binding="{Binding Path=FkAttribute}" Header="Fk Attribute" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="fkVersionColumn" Binding="{Binding Path=FkVersion}" Header="Fk Version" Width="SizeToHeader" />
            </DataGrid.Columns>
        </DataGrid>
        <DataGrid AutoGenerateColumns="False" EnableRowVirtualization="True" Height="109" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource attributeViewSource}}" Margin="12,12,0,0" Name="attributeDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" VerticalAlignment="Top" Width="479">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="iDColumn1" Binding="{Binding Path=ID}" Header="ID" IsReadOnly="True" Width="SizeToHeader" />
                <DataGridTextColumn x:Name="nameColumn1" Binding="{Binding Path=Name}" Header="Name" Width="SizeToHeader" />
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="208,281,0,0" Name="buttonSave" VerticalAlignment="Top" Width="75" Click="buttonSave_Click" />
    </Grid>
</Window>

代码在线失败:

CollectionViewSource equipmentMasterDataSet = ((CollectionViewSource)(this.FindResource("equipmentMasterDataSet")));

我该怎么做才能完成这项工作?如何调试此代码?

4

0 回答 0