Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
D
Dapper
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
Dapper
Commits
5d7d2318
Commit
5d7d2318
authored
Aug 27, 2013
by
Martin Jul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added DeleteAll method to easily delete all entities in a table
parent
bc39863e
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
0 deletions
+30
-0
Tests.cs
Dapper.Contrib.Tests/Tests.cs
+13
-0
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+17
-0
No files found.
Dapper.Contrib.Tests/Tests.cs
View file @
5d7d2318
...
@@ -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
();
}
}
}
}
}
}
Dapper.Contrib/SqlMapperExtensions.cs
View file @
5d7d2318
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment