C#C
C#13mo ago
Jiry_XD

✅ EF Core doesn't stop tracking list items.

Trying to edit an office entity and this works great for everything, except when the editedOffice passes less salesPerson ID's then there were before. If there are less id's it should remove them from the list inside the db aswell, currently it's not doing that. It is however adding new ones correctly when you pass more ids that werent there before.
public async Task<OfficeDTO?> EditOfficeAsync(OfficeEditDTO editedOffice)
{
    var office = await dbContext.Offices.Include(o => o.Address)
                                         .Include(o => o.SalesPeople)
                                         .FirstOrDefaultAsync(o => o.Id == editedOffice.Id);

    if (office == null)
        return null;

    var selectedSalesPeopleIds = office.SalesPeople.Select(o => o.Id).ToList();


    if (editedOffice.Name != null)
        office.Name = editedOffice.Name;

    if(editedOffice.Address != null)
    {
Was this page helpful?