Using System; using System. collections. generic; using System. linq; using System. text; using System. componentModel; using System. threading; using System. IO; using Rocky; namespace Rocky. codeBuilder {public class DbBuilder: Disposable {# region field public event EventHandler DoWork; private BackgroundWorker bgWorker; # endregion # region attribute public event ProgressChangedEventHandler ProgressChanged {add {bgWork Er. progressChanged + = value;} remove {bgWorker. progressChanged-= value ;}} public event RunWorkerCompletedEventHandler RunWorkerCompleted {add {bgWorker. runWorkerCompleted + = value;} remove {bgWorker. runWorkerCompleted-= value ;}} public DbSchema Schema {get; set ;}# endregion # region method public DbBuilder () {bgWorker = new BackgroundWorker (); bgWorker. workerReportsProgress = true; bgWor Ker. workersuppscanscancellation = true; bgWorker. doWork + = new DoWorkEventHandler (bgWorker_DoWork);} protected override void DisposeInternal (bool disposing) {if (disposing) {bgWorker. dispose ();} DoWork = null; bgWorker = null;} void bgWorker_DoWork (object sender, DoWorkEventArgs e) {if (this. schema = null) {this. schema = SystemConfig. instance. getDbSchema (); if (this. schema = null) {bgWork Er. reportProgress (100, "GetDbSchema error. "); return ;}} bgWorker. reportProgress (0, "Working... "); var schema = this. schema. getSchema (); GenerateMode mode = SystemConfig. instance. mode; if (mode & GenerateMode. entity) = GenerateMode. entity) {AppendEntity (schema);} if (mode & GenerateMode. DAC) = GenerateMode. DAC) {AppendDAC (schema);} // if (mode & GenerateMode. tableImage) = GenerateMode. ta BleImage) // {//} bgWorker. ReportProgress (100, "Completed.");} public void RunWorkerAsync () {base. CheckDisposed (); if (DoWork! = Null) {DoWork (this, EventArgs. Empty);} if (! BgWorker. isBusy) {bgWorker. runWorkerAsync () ;}} public void CancelAsync () {if (bgWorker. isBusy) {bgWorker. cancelAsync () ;}# endregion # region Append # region Entity protected void AppendEntity (DbTableSchema [] schema) {string path = SystemConfig. instance. savePath + SystemConfig. instance. folderName + @ "\ Entity \"; Runtime. createDirectory (path); StringBuilder main = new StringBuilder (1600); (Int I = 0; I <schema. length; I ++) {DbTableSchema t_schema = schema [I]; main. appendLine ("using System;"); main. appendLine ("using Rocky. data; "); main. appendLine (); main. appendLine ("namespace" + SystemConfig. instance. nameSpacePrefix + ". entity "); main. appendLine ("{"); main. appendLine ("[Table (\" "+ t_schema.Name +" \ "), Serializable]"); main. appendLine ("public class" + t_schema.Name); main. appen DLine ("{"); for (int j = 0; j <t_schema.Columns.Length; j ++) {DbColumnSchema c_schema = t_schema.Columns [j]; TypeCode typeCode = Type. getTypeCode (c_schema.FieldType); string fieldType = c_schema.FieldType.Name; switch (typeCode) {case TypeCode. string: case TypeCode. object: if (c_schema.FieldType = typeof (Guid) {if (c_schema.IsNullable) {goto default ;}} if (c_schema.IsPrimaryKey) {main. A PpendLine ("[Column (IsNullable = false, IsPrimaryKey = true)]");} else {main. appendLine ("[Column (IsNullable =" + c_schema.IsNullable.ToString (). toLower () + ")]");} main. appendLine ("public" + fieldType + "" + c_schema.Name + "{get; set;}"); main. appendLine (); break; case TypeCode. int32: case TypeCode. int64: if (c_schema.IsNullable) {goto default;} if (c_schema.IsPrimaryKey) {main. appe NdLine ("[Column (IsDbGenerated = true, IsPrimaryKey = true)]");} else {main. appendLine ("[Column (IsNullable = false)]");} main. appendLine ("public" + fieldType + "" + c_schema.Name + "{get; set;}"); main. appendLine (); break; default: if (c_schema.IsNullable) {fieldType + = "? ";} Main. appendLine ("[Column (IsNullable =" + c_schema.IsNullable.ToString (). toLower () + ")]"); main. appendLine ("public" + fieldType + "" + c_schema.Name + "{get; set;}"); main. appendLine (); break;} main. appendLine ("}"); main. appendLine ("}"); File. writeAllText (path + t_schema.Name + ". cs ", main. toString (); main. length = 0; bgWorker. reportProgress (100 * I)/schema. length, t_schema.Name + "Entity"); Thread. sleep (200) ;}# endregion # region Repository protected void AppendDAC (DbTableSchema [] schema) {string path = SystemConfig. instance. savePath + SystemConfig. instance. folderName + @ "\ DAC \"; Runtime. createDirectory (path); path + = "TableComponents. cs "; Runtime. createFileFromResource (CoreHelper. template_TableComponents, path); StringBuilder main = new StringBuilder (); for (int I = 0; I <schema. length; I ++) {DbTableSchema dt = schema [I]; string type = "EntityTable <" + dt. name + ">"; main. appendLine ("public static readonly" + type + "Db" + dt. name + "= new" + type + "();"); bgWorker. reportProgress (100 * I)/schema. length, dt. name + "DAC"); Thread. sleep (200);} File. writeAllText (path, File. readAllText (path ). replace ("_ namespace _", SystemConfig. instance. nameSpacePrefix ). replace ("_ Class _", main. toString ();} # endregion }}