简体   繁体   English

wpf datarow绑定正在保存更改,但不能插入或删除

[英]wpf datarow binding is saving changes but not inserts or deletes

I'm new to wpf so this is a learning curve to me. 我是wpf的新手,所以这对我来说是学习的曲线。 I have the binding working, so any update I do works without an issue. 我有绑定的工作,所以我所做的任何更新都可以正常工作。

The problem is the adds and deletes. 问题是添加和删除。 Neither of them work. 他们都不工作。

Here is my code: 这是我的代码:

using System;
using System.Linq;
using System.Windows;
using WebPortalSourceId.data;

namespace WebPortalSourceId
{
  public partial class MainWindow : Window
  {
    private Guid _corporationId;

    private SuburbanPortalEntities entity;
    public MainWindow()
    {
      InitializeComponent();
      entity = new SuburbanPortalEntities();
    }

    private void Search_Click(object sender, RoutedEventArgs e)
    {
      if (CompanyCode.Text.Length != 3)
      {
        MessageBox.Show("Invalid Company Code. It must be 3 characters in length.");
        return;
      }
      _corporationId = GetCorporationId(CompanyCode.Text.ToUpper());
      FillDataGrid(_corporationId);
    }

    public void FillDataGrid(Guid corporationId)
    {
      var query = from s in entity.Sources where s.CorporationId == corporationId select s;  
      if (query.Any())
      {
        SourceDataGrid.ItemsSource = query.ToList();
      }    
      SourceDataGrid.Columns[2].Visibility = Visibility.Hidden;
      SourceDataGrid.Columns[0].IsReadOnly = true;
      SourceDataGrid.Columns[1].IsReadOnly = true;

    }

    private Guid GetCorporationId(string companycode)
    {
        return (from cs in entity.CorporationStructures
          where cs.Company == companycode &
                cs.IsActive & 
                cs.Branch == null
          select cs.CorporationId).FirstOrDefault();
    }

    private void Save_Click(object sender, RoutedEventArgs e)
    {
      entity.SaveChanges();
    }

    private void SourceDataGrid_AddingNewItem(object sender, System.Windows.Controls.AddingNewItemEventArgs e)
    {
      var sources = new Source();
      sources.CorporationId = _corporationId;
      sources.Description = string.Empty;
      sources.IsActive = true;
      sources.Name = string.Empty;
      sources.SourceId = Guid.NewGuid();
      sources.TokenId = Guid.NewGuid();
      e.NewItem = sources;
    }

  }

}

And my xaml: 而我的xaml:

<Window x:Class="WebPortalSourceId.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Web Portal SourceId" Height="475" Width="948" WindowStartupLocation="CenterScreen" ResizeMode="NoResize">
  <Grid>
    <TextBox Name="CompanyCode" HorizontalAlignment="Left" Height="23" Margin="337,11,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="48" MaxLength="3" TextAlignment="Center" CharacterCasing="Upper"/>
    <Label Content="Company Code" HorizontalAlignment="Left" Margin="240,10,0,0" VerticalAlignment="Top"/>
    <DataGrid Name="SourceDataGrid" Margin="10,43,10,10" CanUserReorderColumns="False" CanUserResizeRows="False" HorizontalContentAlignment="Stretch" FontFamily="Microsoft YaHei" AddingNewItem="SourceDataGrid_AddingNewItem" SelectionMode="Single" SelectionUnit="CellOrRowHeader"/>
    <Button Name="Search" Content="Search" HorizontalAlignment="Left" Margin="390,11,0,0" VerticalAlignment="Top" Width="75" Height="23" Click="Search_Click"/>
    <Button x:Name="Save" Content="Save" Margin="470,11,397,0" VerticalAlignment="Top" Height="23" Click="Save_Click" HorizontalContentAlignment="Center"/>


  </Grid>
</Window>

What am I missing? 我想念什么?

public void FillDataGrid(Guid corporationId)
{
  var query = from s in entity.Sources where s.CorporationId == corporationId select s;
  query.Load(); 

  SourceDataGrid.ItemsSource = entity.Sources.Local;  
  SourceDataGrid.Columns[2].Visibility = Visibility.Hidden;
  SourceDataGrid.Columns[0].IsReadOnly = true;
  SourceDataGrid.Columns[1].IsReadOnly = true;

}

Queryable<T>.ToList() creates a List that Entity Framework knows nothing about. Queryable<T>.ToList()创建一个List ,Entity Framework对此一无所知。 However EF still keeps a reference to each SourceProxy item it instanciated from the database. 但是,EF仍然保留对它从数据库实例化的每个SourceProxy项的引用。

By using DbSet<T>.Local we get an ObservableCollection , which EF has bound to for inserts and deletes. 通过使用DbSet<T>.Local我们得到一个ObservableCollection ,EF绑定了该ObservableCollection进行插入和删除。 However be advised, it is fracking slow. 但是请注意,它的压裂速度很慢。 Not to mention, all access to it now must be done via the UI thread. 更不用说,对它的所有访问现在都必须通过UI线程完成。

So even DB access to DbSet<T> must be done via the UI thread. 因此,甚至DB都必须通过UI线程来访问DbSet<T> Which is bad. 不好

A better way to deal with this is to create you own ObservableCollection , and handle the Remove / Add yourself. 解决此问题的更好方法是创建您自己的ObservableCollection ,然后自行处理Remove / Add

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

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