private RelationshipModel CreateRelationshipModel(DataRow[] relationshipRows) { RelationshipModel relationshipModel = new RelationshipModel(); relationshipModel.Name = Convert.ToString(relationshipRows[0]["ForeignKeyName"]); relationshipModel.PrincipalEnd = new EndModel(Convert.ToString(relationshipRows[0]["PrincipalTableSchema"]), Convert.ToString(relationshipRows[0]["PrincipalTableName"]), relationshipRows.Select(p => Convert.ToString(p["PrincipalColumnName"])).ToArray()); relationshipModel.DependentEnd = new EndModel(Convert.ToString(relationshipRows[0]["DependentTableSchema"]), Convert.ToString(relationshipRows[0]["DependentTableName"]), relationshipRows.Select(p => Convert.ToString(p["DependentColumnName"])).ToArray()); relationshipModel.UpdateCascade = Convert.ToBoolean(relationshipRows[0]["UpdateCascade"]); relationshipModel.DeleteCascade = Convert.ToBoolean(relationshipRows[0]["DeleteCascade"]); return(relationshipModel); }
public DatabaseModel CreateDatabaseModel() { DataSet dataSet = new DataSet(); string[] scripts = new string[] { DatabaseHelper.GenerateLoadTablesScript(), DatabaseHelper.GenerateLoadTableColumnsScript(), DatabaseHelper.GenerateLoadViewsScript(), DatabaseHelper.GenerateLoadViewColumnsScript(), DatabaseHelper.GenerateLoadIdentitiesScript(), DatabaseHelper.GenerateLoadIndicesScript(), DatabaseHelper.GenerateLoadForeignKeysScript() }; using (SqlConnection connection = new SqlConnection(this._connectionString)) { connection.Open(); SqlDataAdapter adapter = new SqlDataAdapter(string.Join(";", scripts), connection); adapter.Fill(dataSet); this._tTables = dataSet.Tables[0]; this._tTableColumns = dataSet.Tables[1]; this._tViews = dataSet.Tables[2]; this._tViewColumns = dataSet.Tables[3]; this._tIdentities = dataSet.Tables[4]; this._tIndices = dataSet.Tables[5]; this._tForeignKeys = dataSet.Tables[6]; DatabaseModel databaseModel = new DatabaseModel(connection.Database); // Tables foreach (DataRow row in this._tTables.Rows) { TableModel tableModel = this.CreateTableModel(row); databaseModel.Tables.Add(tableModel); } // Relationships foreach (IGrouping <string, DataRow> row in this._tForeignKeys.Select().GroupBy(p => Convert.ToString(p["ForeignKeyName"]))) { RelationshipModel relationshipModel = this.CreateRelationshipModel(row.ToArray()); databaseModel.Relationships.Add(relationshipModel); } // Views foreach (DataRow row in this._tViews.Rows) { ViewModel viewModel = this.CreateViewModel(row); databaseModel.Views.Add(viewModel); } return(databaseModel); } }