Entity Framework ChangeTracker 未更新,returns "deleted" 个实体
Entity Framework ChangeTracker not updated, returns "deleted" entities
我成功地通过数据网格删除行事件从 SQLite 数据库中删除了实体。这可以通过 SQLite Manager 确认。然而,在 运行 这个删除事件和 SaveChanges() 命令之后,我仍然在使用 GetLocal() 方法后得到删除的实体。
这是 deleteRow 方法(Complete() 调用 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方法returns之前删除的所有实体?
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);
仍然是 returns 之前删除的旧实体?
这是设备数据库table:
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 当前保留代表已删除实体的条目。这是针对更改跟踪器中的某些边缘情况特意完成的,尽管这可能会在未来的 EF 更新中更改。
您可以通过筛选列表来避免选择删除实体的反向更改跟踪器条目。
Context
.ChangeTracker
.Entries<TEntity>()
.Where(e => e.State != EntityState.Detached)
.Select(e => e.Entity);
我成功地通过数据网格删除行事件从 SQLite 数据库中删除了实体。这可以通过 SQLite Manager 确认。然而,在 运行 这个删除事件和 SaveChanges() 命令之后,我仍然在使用 GetLocal() 方法后得到删除的实体。
这是 deleteRow 方法(Complete() 调用 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方法returns之前删除的所有实体?
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);
仍然是 returns 之前删除的旧实体?
这是设备数据库table:
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 当前保留代表已删除实体的条目。这是针对更改跟踪器中的某些边缘情况特意完成的,尽管这可能会在未来的 EF 更新中更改。
您可以通过筛选列表来避免选择删除实体的反向更改跟踪器条目。
Context
.ChangeTracker
.Entries<TEntity>()
.Where(e => e.State != EntityState.Detached)
.Select(e => e.Entity);