public void DisableAutoIncrement(ISqlDataStore dataStore) { if (!HasAutoIncrement) { return; } dataStore.ExecuteNonQuery(string.Format(CultureInfo.InvariantCulture, "SET IDENTITY_INSERT [{0}] ON;", EntityName)); }
public void RestoreAutoIncrement(ISqlDataStore datastore) { if (!HasAutoIncrement) { return; } datastore.ExecuteNonQuery(string.Format(CultureInfo.InvariantCulture, "SET IDENTITY_INSERT [{0}] OFF;", EntityName)); var primaryKey = Fields.FirstOrDefault(_ => _.IsPrimaryKey); if (primaryKey == null) { return; } var primaryKeyField = primaryKey.Name; var maxIdDbValue = datastore.ExecuteScalar(string.Format("SELECT MAX({0}) FROM [{1}]", primaryKeyField, EntityName)); var maxId = maxIdDbValue == DBNull.Value ? 1 : (long)maxIdDbValue; datastore.ExecuteNonQuery(string.Format("ALTER TABLE [{0}] ALTER COLUMN {1} IDENTITY ({2}, 1);", EntityName, primaryKeyField, maxId + 1)); }
public void ApplyDelete(ISqlDataStore datastore, string entityName, EntityChangesetBuilder entitySerialiazer) { try { using (var command = entitySerialiazer.ToDelete(datastore, entityName, this)) { datastore.ExecuteNonQuery(command); } } catch (Exception e) { throw new Exception(string.Format("Erreur lors de la suppression {0}. Exception: {1}", this, e)); } }
public void ApplyInsert(ISqlDataStore datastore, EntityChangesetBuilder entitySerialiazer) { try { DisableAutoIncrement(datastore); using (var command = entitySerialiazer.ToInsert(datastore, EntityName, this)) { datastore.ExecuteNonQuery(command); } } catch (Exception e) { throw new Exception(string.Format("Erreur lors de l'insertion {0}. Exception: {1}", this, e)); } finally { RestoreAutoIncrement(datastore); } }