public void ValidateTable(IEntityInfo entity) { var entityName = entity.GetNameInStore(); var connection = _datastore.GetConnection(); // first make sure the table exists if (_datastore.TableExists(entityName)) { return; } _datastore.CreateTable(connection, entity); }
public void ValidateTable(IEntityInfo entity) { var entityName = entity.GetNameInStore(); var connection = _datastore.GetConnection(); // first make sure the table exists if (!_datastore.TableExists(entityName)) { _datastore.CreateTable(connection, entity); return; } using (var command = new SqlCeCommand()) { command.Transaction = _datastore.CurrentTransaction as SqlCeTransaction; command.Connection = connection as SqlCeConnection; foreach (var field in entity.Fields) { // yes, I realize hard-coded ordinals are not a good practice, but the SQL isn't changing, it's method specific var sql = string.Format("SELECT column_name, " // 0 + "data_type, " // 1 + "character_maximum_length, " // 2 + "numeric_precision, " // 3 + "numeric_scale, " // 4 + "is_nullable " + "FROM information_schema.columns " + "WHERE (table_name = '{0}' AND column_name = '{1}')", entityName, field.FieldName); command.CommandText = sql; using (var reader = command.ExecuteReader()) { if (!reader.Read()) { // field doesn't exist - we must create it var alter = new StringBuilder(string.Format("ALTER TABLE [{0}] ", entity.GetNameInStore())); alter.Append(string.Format("ADD {0}", field.GetFieldDefinitionSqlQuery())); using (var altercmd = new SqlCeCommand(alter.ToString(), connection as SqlCeConnection)) { altercmd.ExecuteNonQuery(); } } } } _datastore.VerifiyPrimaryKey(entity.PrimaryKey); foreach (var foreignKey in entity.ForeignKeys) { _datastore.VerifyForeignKey(foreignKey); } foreach (var index in entity.Indexes) { _datastore.VerifyIndex(index); } } }