Commit 5d7d2318 authored by Martin Jul's avatar Martin Jul

Added DeleteAll method to easily delete all entities in a table

parent bc39863e
...@@ -181,5 +181,18 @@ public void InsertFieldWithReservedName() ...@@ -181,5 +181,18 @@ public void InsertFieldWithReservedName()
} }
} }
public void DeleteAll()
{
using (var connection = GetOpenConnection())
{
var id1 = connection.Insert(new User() { Name = "Alice", Age = 32 });
var id2 = connection.Insert(new User() { Name = "Bob", Age = 33 });
connection.DeleteAll<User>();
connection.Get<User>(id1).IsNull();
connection.Get<User>(id2).IsNull();
}
}
} }
} }
...@@ -280,6 +280,23 @@ private static string GetTableName(Type type) ...@@ -280,6 +280,23 @@ private static string GetTableName(Type type)
return deleted > 0; return deleted > 0;
} }
/// <summary>
/// Delete all entities in the table related to the type T.
/// </summary>
/// <typeparam name="T">Type of entity</typeparam>
/// <param name="connection">Open SqlConnection</param>
/// <returns>true if deleted, false if none found</returns>
public static bool DeleteAll<T>(this IDbConnection connection, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var type = typeof(T);
var name = GetTableName(type);
var statement = String.Format("delete from {0}", name);
var deleted = connection.Execute(statement, null, transaction: transaction, commandTimeout: commandTimeout);
return deleted > 0;
}
public static ISqlAdapter GetFormatter(IDbConnection connection) public static ISqlAdapter GetFormatter(IDbConnection connection)
{ {
string name = connection.GetType().Name.ToLower(); string name = connection.GetType().Name.ToLower();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment