Skip to content

CRUD Deleting

berkeleybross edited this page Mar 19, 2018 · 2 revisions
void Delete<TEntity>(TEntity entity, int? commandTimeout = null, bool? verifyAffectedRowCount = null)

Deletes the given entity by using it's primary key.

Throws AffectedRowCountException if the delete command didn't delete anything, or deleted multiple records, and verifyAffectedRowCount (or it's default) is true

void Delete<TEntity>(object id, int? commandTimeout = null, bool? verifyAffectedRowCount = null);

Deletes the entity who's primary key matches the given id.

Throws AffectedRowCountException if the delete command didn't delete anything, or deleted multiple records, and verifyAffectedRowCount (or it's default) is true

Examples

Given the POCO class:

[Table("Users")]
public class UserEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Delete an entity

var entity = database.Find<User>(5);

database.Delete(entity);
// or
await database.DeleteAsync(entity);

MS-SQL 2012 +

DELETE FROM [Users]
WHERE [Id] = @Id

PostgreSQL

DELETE FROM user
WHERE id = @Id

Delete an entity by id

database.Delete<User>(5);
// or
await database.DeleteAsync<User>(5);
MS-SQL 2012 +
```SQL
DELETE FROM [Users]
WHERE [Id] = @Id

PostgreSQL

DELETE FROM user
WHERE id = @Id

Deleting many entities

SqlCommandResult DeleteRange<TEntity>(string conditions, object parameters = null, int? commandTimeout = null);

Deletes the entities in the table which match the given conditions.

⚠️ For safety, the conditions parameter must begin with a WHERE clause. If you don't want a WHERE clause, then you must use DeleteAll<TEntity>

SqlCommandResult DeleteRange<TEntity>(object conditions, int? commandTimeout = null);

Deletes the entities in the table which match the given conditions. The conditions should be an anonymous object whose properties match those of TEntity. All properties defined on the conditions will be combined with an AND clause. If the value of a property is null then the SQL generated will check for IS NULL.

SqlCommandResult DeleteAll<TEntity>(int? commandTimeout = null);

Deletes all entities in the table.

📝 This performs a SQL Delete statement not a TRUNCATE statement.

Examples

Given the POCO class:

[Table("Users")]
public class UserEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
}

Delete all users whos name contain "Foo"

database.DeleteRange<UserEntity>("WHERE Name LIKE '%Foo%'");
// or
await database.DeleteRangeAsync<UserEntity>("WHERE Name LIKE '%Foo%'");

MS-SQL 2012 +

DELETE FROM [Users]
WHERE Name LIKE '%Foo%'

PostgreSQL

DELETE FROM Users
WHERE Name LIKE '%Foo%'

Delete all users who are 18

database.DeleteRange<UserEntity>(new { Age = 18 });
// or
await database.DeleteRangeAsync<UserEntity>(new { Age = 18 });

MS-SQL 2012 +

DELETE FROM [Users]
WHERE [Age] = @Age

PostgreSQL

DELETE FROM user
WHERE age = @Age

Delete all users

database.DeleteAll<UserEntity>();
// or
await database.DeleteAllAsync<UserEntity>();

MS-SQL 2012 +

DELETE FROM [Users]

PostgreSQL

DELETE FROM user