static void InsertElement(this SQLiteConnection conn, object element, bool replace, PropertyInfo primaryKeyProperty, bool isAutoIncrementPrimaryKey, ISet <object> objectCache) { if (element == null || (objectCache != null && objectCache.Contains(element))) { return; } bool isPrimaryKeySet = false; if (replace && isAutoIncrementPrimaryKey) { var primaryKeyValue = primaryKeyProperty.GetValue(element, null); var defaultPrimaryKeyValue = primaryKeyProperty.PropertyType.GetDefault(); isPrimaryKeySet = primaryKeyValue != null && !primaryKeyValue.Equals(defaultPrimaryKeyValue); } bool shouldReplace = replace && (!isAutoIncrementPrimaryKey || isPrimaryKeySet); // Only replace elements that have an assigned primary key if (shouldReplace) { conn.InsertOrReplace(element); } else { conn.Insert(element); } }