Commit d35b72e3 authored by Sam Saffron's avatar Sam Saffron

the transaction is not really needed there ... it helps with nothing.

parent b60c0a53
...@@ -146,44 +146,42 @@ private static string GetTableName(Type type) ...@@ -146,44 +146,42 @@ private static string GetTableName(Type type)
/// <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
{ {
using (var tx = connection.BeginTransaction())
{ var type = typeof(T);
var type = typeof(T);
var name = GetTableName(type); var name = GetTableName(type);
var sb = new StringBuilder(null); var sb = new StringBuilder(null);
sb.AppendFormat("insert into {0} (", name); sb.AppendFormat("insert into {0} (", name);
var allProperties = TypePropertiesCache(type); var allProperties = TypePropertiesCache(type);
var keyProperties = KeyPropertiesCache(type); var keyProperties = KeyPropertiesCache(type);
for (var i = 0; i < allProperties.Count(); i++) for (var i = 0; i < allProperties.Count(); i++)
{ {
var property = allProperties.ElementAt(i); var property = allProperties.ElementAt(i);
if (keyProperties.Contains(property)) continue; if (keyProperties.Contains(property)) continue;
sb.Append(property.Name); sb.Append(property.Name);
if (i < allProperties.Count() - 1) if (i < allProperties.Count() - 1)
sb.Append(", "); sb.Append(", ");
} }
sb.Append(") values ("); sb.Append(") values (");
for (var i = 0; i < allProperties.Count(); i++) for (var i = 0; i < allProperties.Count(); i++)
{ {
var property = allProperties.ElementAt(i); var property = allProperties.ElementAt(i);
if (keyProperties.Contains(property)) continue; if (keyProperties.Contains(property)) continue;
sb.AppendFormat("@{0}", property.Name); sb.AppendFormat("@{0}", property.Name);
if (i < allProperties.Count() - 1) if (i < allProperties.Count() - 1)
sb.Append(", "); sb.Append(", ");
}
sb.Append(") ");
connection.Execute(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
var r = connection.Query("select @@IDENTITY id");
tx.Commit();
return (int)r.First().id;
} }
sb.Append(") ");
connection.Execute(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
var r = connection.Query("select @@IDENTITY id", transaction: transaction, commandTimeout: commandTimeout);
return (int)r.First().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