Sto eliminando con successo l'entità dal database SQLite dall'evento di riga di eliminazione di datagrid. Questo può essere confermato da SQLite Manager. Tuttavia, dopo aver eseguito questo evento di eliminazione e il comando SaveChanges () ho ancora le entità eliminate dopo aver usato il metodo GetLocal () .
Ecco il metodo deleteRow ( Complete () chiama 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();
}
}
}
La prossima volta caricando le entità ottengo le entità corrette, ma dopo aver eseguito GetLocal () ricevo le entità cancellate in precedenza?
Caricamento in corso:
_context.Devices.Load();
var devices = _context.Devices.GetLocal();
DeviceListDataGrid.ItemsSource = devices;
Il metodo GetLocal restituisce tutte le entità cancellate in precedenza?
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;
}
Per qualche motivo la riga var data = Context.ChangeTracker.Entries<TEntity>().Select(e => e.Entity);
restituisce ancora le vecchie entità prima della cancellazione?
Ecco la tabella del database dei dispositivi:
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
)
ChangeTracker contiene attualmente voci che rappresentano entità eliminate. Questo è stato fatto deliberatamente per alcuni casi limite nel tracker dei cambiamenti, sebbene questo possa essere modificato nei futuri aggiornamenti di EF.
È possibile evitare di selezionare le voci di modifica del tracker di eliminare le entità filtrando l'elenco.
Context
.ChangeTracker
.Entries<TEntity>()
.Where(e => e.State != EntityState.Detached)
.Select(e => e.Entity);