Commit 71258e75 authored by David Chell's avatar David Chell

Updated SqlServerAdapter to write keys back to object after inserting it into the database.

parent d57970fb
...@@ -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
......
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