Commit 64666d5c authored by David Chell's avatar David Chell

Added postresql support for Insert to return primary key of inserted row.

parent 58563dfb
...@@ -145,7 +145,7 @@ private static string GetTableName(Type type) ...@@ -145,7 +145,7 @@ private static string GetTableName(Type type)
/// <param name="entityToInsert">Entity to insert</param> /// <param name="entityToInsert">Entity to insert</param>
/// <returns>Identity of inserted entity</returns> /// <returns>Identity of inserted entity</returns>
public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class public static long Insert<T>(this IDbConnection connection, T entityToInsert, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{ {
var type = typeof(T); var type = typeof(T);
...@@ -174,12 +174,17 @@ private static string GetTableName(Type type) ...@@ -174,12 +174,17 @@ private static string GetTableName(Type type)
if (i < allPropertiesExceptKey.Count() - 1) if (i < allPropertiesExceptKey.Count() - 1)
sb.Append(", "); sb.Append(", ");
} }
sb.Append(") "); sb.Append(") RETURNING * ");
connection.Execute(sb.ToString(), entityToInsert, transaction: transaction, commandTimeout: commandTimeout); var r = connection.Query(sb.ToString(), entityToInsert, transaction: transaction, commandTimeout: commandTimeout);
//NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE long id = 0;
var r = connection.Query("select @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout); foreach (var p in keyProperties)
return (int)r.First().id; {
var value = ((IDictionary<string, object>)r.First())[p.Name.ToLower()];
p.SetValue(entityToInsert, value, null);
if (id == 0)
id = Convert.ToInt64(value);
}
return id;
} }
/// <summary> /// <summary>
......
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