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
71258e75
Commit
71258e75
authored
Mar 12, 2012
by
David Chell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Updated SqlServerAdapter to write keys back to object after inserting it into the database.
parent
d57970fb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
8 deletions
+23
-8
SqlMapperExtensions.cs
Dapper.Contrib/SqlMapperExtensions.cs
+23
-8
No files found.
Dapper.Contrib/SqlMapperExtensions.cs
View file @
71258e75
...
@@ -486,7 +486,15 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
...
@@ -486,7 +486,15 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
var
r
=
connection
.
Query
(
"select @@IDENTITY id"
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
return
(
int
)
r
.
First
().
id
;
int
id
=
0
;
foreach
(
var
p
in
keyProperties
)
{
var
value
=
((
IDictionary
<
string
,
object
>)
r
.
First
())[
p
.
Name
.
ToLower
()];
p
.
SetValue
(
entityToInsert
,
value
,
null
);
if
(
id
==
0
)
id
=
Convert
.
ToInt32
(
value
);
}
return
id
;
}
}
}
}
...
@@ -497,15 +505,22 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
...
@@ -497,15 +505,22 @@ public int Insert(IDbConnection connection, IDbTransaction transaction, int? com
StringBuilder
sb
=
new
StringBuilder
();
StringBuilder
sb
=
new
StringBuilder
();
sb
.
AppendFormat
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
sb
.
AppendFormat
(
"insert into {0} ({1}) values ({2})"
,
tableName
,
columnList
,
parameterList
);
sb
.
Append
(
" RETURNING "
);
// If no primary key then safe to assume a join table with not too much data to return
bool
first
=
true
;
if
(!
keyProperties
.
Any
())
foreach
(
var
property
in
keyProperties
)
sb
.
Append
(
" RETURNING *"
);
else
{
{
if
(!
first
)
sb
.
Append
(
" RETURNING "
);
sb
.
Append
(
", "
);
bool
first
=
true
;
first
=
false
;
foreach
(
var
property
in
keyProperties
)
sb
.
Append
(
property
.
Name
);
{
if
(!
first
)
sb
.
Append
(
", "
);
first
=
false
;
sb
.
Append
(
property
.
Name
);
}
}
}
var
results
=
connection
.
Query
(
sb
.
ToString
(),
entityToInsert
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
var
results
=
connection
.
Query
(
sb
.
ToString
(),
entityToInsert
,
transaction
:
transaction
,
commandTimeout
:
commandTimeout
);
// Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys
// Return the key by assinging the corresponding property in the object - by product is that it supports compound primary keys
...
...
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