_context.Entry(tmp).CurrentValues.SetValues(toUpdate); entity Id error

public async Task<bool> Update(T toUpdate, int Id)
{
    T? tmp = await _entities.SingleOrDefaultAsync(x => x.Id == Id);
    if (tmp != null)
    {
        await Task.Run(() => _context.Entry(tmp).CurrentValues.SetValues(toUpdate));
        await _context.SaveChangesAsync();
        return true;
    }
    return false;
}

Ошибка:

System.InvalidOperationException: 'The property 'Book.Id' is part of a key and so cannot be modified or marked as modified. To change the principal of an existing entity with an identifying foreign key, first delete the dependent and invoke 'SaveChanges', and then associate the dependent with the new principal.'


Ответы (0 шт):