diff --git a/src/SQLite.cs b/src/SQLite.cs index 0ce075e0..c1eac952 100644 --- a/src/SQLite.cs +++ b/src/SQLite.cs @@ -1710,21 +1710,20 @@ PreparedSqlLiteInsertCommand GetInsertCommand (TableMapping map, string extra) var key = Tuple.Create (map.MappedType.FullName, extra); lock (_insertCommandMap) { - _insertCommandMap.TryGetValue (key, out prepCmd); + if (_insertCommandMap.TryGetValue (key, out prepCmd)) { + return prepCmd; + } } - if (prepCmd == null) { - prepCmd = CreateInsertCommand (map, extra); - var added = false; - lock (_insertCommandMap) { - if (!_insertCommandMap.ContainsKey (key)) { - _insertCommandMap.Add (key, prepCmd); - added = true; - } - } - if (!added) { + prepCmd = CreateInsertCommand (map, extra); + + lock (_insertCommandMap) { + if (_insertCommandMap.TryGetValue (key, out var existing)) { prepCmd.Dispose (); + return existing; } + + _insertCommandMap.Add (key, prepCmd); } return prepCmd;