How to remove record from the database with the foreign key set to NULL

Viewed 31

I have a c# code to delete one record from the SQLite table. After deleting the record with account.UserFunctions.Remove(toRemove); the record stays in the database with the foreign key set to NULL. How can I completely remove that record from the database programmatically.

public AccountResponse DeleteFunction(int id, UpdateUserFunctionRequest functionReq)
{
    var account = getAccount(id);

    Function toRemove = null;
    // Purge all schedules & UserFunctions  - we don't know which were changed
    foreach (var item in account.UserFunctions)
    {

        if (item.UserFunction == functionReq.UserFunction)
        {
            toRemove = item;
            break; // Found
        }

    }
    if(toRemove != null)
    {
        _context.UserFunctions.Attach(toRemove);
        account.UserFunctions.Remove(toRemove);
        _context.SaveChanges();
    }

    //account.Updated = DateTime.UtcNow;
    //_context.UserFunctions.RemoveRange(_context.UserFunctions);

    return _mapper.Map<AccountResponse>(account);
}
0 Answers
Related