'Entity Framework tracks records as deleted without removing
I have the following code. I only query data and do not remove or delete entities, but EF tracks the record as deleted. I don't understand why.
List<SubscriptionModuleDto> subscriptionModuleDtos = new List<SubscriptionModuleDto>();
/// Here I get just 1 model
List<SoftwareModule> calcModuleModels =
await _managementContext.SoftwareModule.Where(s => s.SoftwareId == (int)softwareId && s.Part.Id == 70)
.Include(s => s.Software)
.Include(s => s.Part)
.ToListAsync();
/// If I execute the next code, the model from the query above will be tracked as deleted.
List<SoftwareModule> pmModuleModules =
await _managementContext.SoftwareModule.Where(s => s.SoftwareId == subscriptionModel.PackageIdProjectManagement && s.Part.Id == 70)
.Include(s => s.Software)
.Include(s => s.Part)
.ToListAsync();
// here I filter the unchanged entities / models.
foreach (var model in calcModuleModels)
{
if (_managementContext.Entry(model).State != EntityState.Unchanged)
{
bool x = true;
throw new Exception("model will be deleted");
}
}```
How does EF code know that the records will be deleted? How does EF core work? I don't understand.
Solution 1:[1]
The problem is solved. The .Include(s.Part) had a one to one relationship instead of a one to many. –
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mr r. |
