2016-02-26 210 views
0

我正在通过datagrid删除行事件从SQLite数据库成功删除实体。这可以由SQLite Manager确认。然而,在运行这个删除事件并且使用GetLocal()方法后,我仍然获得了删除的实体。实体框架ChangeTracker未更新,返回“已删除”实体

这里的deleteRow方法即可(完成()调用的SaveChanges()):

private void dld_PreviewKeyDown(object sender, KeyEventArgs e) 
{ 
    if (e.Key == Key.Delete && !_isBeingEdited) 
    { 
     var grid = (DataGrid)sender; 
     if (grid.SelectedItems.Count > 0) 
     { 
      var res = MessageBox.Show("Are you sure you want to delete " + grid.SelectedItems.Count + " devices?", "Deleting Records", MessageBoxButton.YesNo, MessageBoxImage.Exclamation); 
      if (res == MessageBoxResult.Yes) 
      { 
       foreach (var row in grid.SelectedItems) 
       { 
        Device device = row as Device; 
        _context.Devices.RemoveDevice(device); 
       } 
       _context.Complete(); 
       MessageBox.Show(grid.SelectedItems.Count + " Devices have being deleted!"); 
      } 
      else 
       DeviceListDataGrid.ItemsSource = _context.Devices.GetLocal(); 
     } 
    } 
} 

下一次加载实体时,我得到了正确的实体,但运行的getLocal()后,我收到了以前删除的实体?

加载:

_context.Devices.Load(); 
    var devices = _context.Devices.GetLocal(); 
    DeviceListDataGrid.ItemsSource = devices; 

方法的getLocal返回所有以前删除的实体?

public ObservableCollection<TEntity> GetLocal() 
{ 
    Context.GetService<DbContext>(); 
    var data = Context.ChangeTracker.Entries<TEntity>().Select(e => e.Entity); 
    var collection = new ObservableCollection<TEntity>(data); 

    collection.CollectionChanged += (s, e) => 
    { 
     if (e.NewItems != null) 
     { 
      Context.AddRange(e.NewItems.Cast<TEntity>()); 
     } 

     if (e.OldItems != null) 
     { 
      Context.RemoveRange(e.OldItems.Cast<TEntity>()); 
     } 
    }; 

    return collection; 
} 

出于某种原因,线var data = Context.ChangeTracker.Entries<TEntity>().Select(e => e.Entity);仍返回旧的实体之前删除?

这里的设备数据库表:

CREATE TABLE "Device" (
    "DeviceId" INTEGER NOT NULL CONSTRAINT "PK_Device" PRIMARY KEY AUTOINCREMENT, 
    "AdditionalInformation" TEXT, 
    "Ampere" REAL, 
    "Category" TEXT, 
    "Category1CategoryId" INTEGER, 
    "Description" TEXT, 
    "DocumentIdentifier" TEXT, 
    "GrossPrice" REAL, 
    "HasErrors" INTEGER NOT NULL, 
    "IsValid" INTEGER NOT NULL, 
    "LeafletPath" TEXT, 
    "Location" TEXT, 
    "Name" TEXT, 
    "NetPrice" REAL, 
    "Notes" TEXT, 
    "ProductCode" INTEGER NOT NULL, 
    "ProductType" TEXT, 
    "ProductType1ProductTypeId" INTEGER, 
    "Supplier" TEXT, 
    "Supplier1SupplierId" INTEGER, 
    "TechData" TEXT, 
    "TimeCreated" TEXT NOT NULL, 
    "UseDefaultValuesFlag" INTEGER, 
    "Watt" REAL, 
    CONSTRAINT "FK_Device_Category_Category1CategoryId" FOREIGN KEY ("Category1CategoryId") REFERENCES "Category" ("CategoryId") ON DELETE RESTRICT, 
    CONSTRAINT "FK_Device_ProductType_ProductType1ProductTypeId" FOREIGN KEY ("ProductType1ProductTypeId") REFERENCES "ProductType" ("ProductTypeId") ON DELETE RESTRICT, 
    CONSTRAINT "FK_Device_Supplier_Supplier1SupplierId" FOREIGN KEY ("Supplier1SupplierId") REFERENCES "Supplier" ("SupplierId") ON DELETE RESTRICT 
) 

回答

1

ChangeTracker目前持有上表示删除实体条目。这是针对变更跟踪器中的某些边缘案例进行的,尽管在将来对EF进行更新时可能会对此进行更改。

您可以通过过滤列表来避免选择删除实体的更改跟踪器条目。

Context 
    .ChangeTracker 
    .Entries<TEntity>() 
    .Where(e => e.State != EntityState.Detached) 
    .Select(e => e.Entity);