public override int Run(string[] args) { SchemaImporter importer = null; switch (DatabaseType) { case "mssql": case "sqlserver": importer = new MSSqlSchemaImporter(); break; } SchemaInfo schemaInfo = importer.GetSchemaFromDatabase(this); AutoDetectRelations(schemaInfo); schemaInfo.Resolve(); RemoveReferencePrimaryKeys(schemaInfo); AutoDetectCollections(schemaInfo); DataSourceInfo dsi = new DataSourceInfo(); dsi.Name = "default"; dsi.DataSourceType = "Sooda.Sql.SqlDataSource"; schemaInfo.DataSources.Add(dsi); foreach (ClassInfo ci in schemaInfo.Classes) { int pkCount = 0; foreach (FieldInfo fi in ci.LocalTables[0].Fields) { if (fi.IsPrimaryKey) { pkCount++; } } if (pkCount == 0) { Console.WriteLine("WARNING: Table {0} doesn't have a primary key.", ci.Name); } } XmlSerializer ser = new XmlSerializer(typeof(SchemaInfo)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", SchemaInfo.XmlNamespace); using (FileStream fs = File.Create(OutputFile)) { ser.Serialize(fs, schemaInfo, ns); } return(0); }
public override int Run(string[] args) { SchemaImporter importer = null; switch (DatabaseType) { case "mssql": case "sqlserver": importer = new MSSqlSchemaImporter(); break; } SchemaInfo dbSchemaInfo = importer.GetSchemaFromDatabase(this); XmlSerializer ser = new XmlSerializer(typeof(SchemaInfo)); SchemaInfo currentSchemaInfo; using (FileStream fs = File.OpenRead(SchemaFile)) { currentSchemaInfo = (SchemaInfo)ser.Deserialize(fs); } if (UpdateTypes || UpdateSizes || UpdateNullable || UpdatePrimaryKeys) { foreach (ClassInfo ci in currentSchemaInfo.Classes) { foreach (TableInfo ti in ci.LocalTables) { if (TableName == null || ti.DBTableName == TableName) { foreach (FieldInfo fi in ti.Fields) { UpdateFieldFromDbSchema(ti, fi, dbSchemaInfo); } } } } } if (OutputSchemaFile == null) { string oldFile = Path.ChangeExtension(SchemaFile, ".old"); if (File.Exists(oldFile)) { File.Delete(oldFile); } File.Move(SchemaFile, oldFile); using (FileStream fs = File.Create(SchemaFile)) { ser.Serialize(fs, currentSchemaInfo); } } else { using (FileStream fs = File.Create(OutputSchemaFile)) { ser.Serialize(fs, currentSchemaInfo); } } return(0); }