-
Notifications
You must be signed in to change notification settings - Fork 4
CRUD Deleting
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
Given the POCO class:
[Table("Users")]
public class UserEntity
{
public int Id { get; set; }
public string Name { get; set; }
}
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
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
SqlCommandResult DeleteRange<TEntity>(string conditions, object parameters = null, int? commandTimeout = null);
Deletes the entities in the table which match the given conditions.
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.
Given the POCO class:
[Table("Users")]
public class UserEntity
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
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%'
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
database.DeleteAll<UserEntity>();
// or
await database.DeleteAllAsync<UserEntity>();
MS-SQL 2012 +
DELETE FROM [Users]
PostgreSQL
DELETE FROM user