NCodeGenerate. the classes in the DBSchema Assembly correspond to various objects in the database, including database (DatabaseSchema), table (TableSchema), column (ColumnSchema), primary key (PrimaryKeySchema), and key (TableKeySchema ), viewSchema and CommandSchema.
Below is a small Demo, which traverses all the fields of all tables in the following database and prints them out.
1. Create a template
Enter the following content in the model:
1 using System; 2 using System.ComponentModel; 3 4 using NCodeGenerateIDE; 5 using NCodeGenerate.DBSchema; 6 namespace DynamicCodeGenerate 7 { 8 public class myModel 9 {10 11 private System.String _Namespace = string.Empty;12 public System.String Namespace13 {14 get { return _Namespace; }15 set { _Namespace = value; }16 }17 private System.String _RemoveTablePrefix = string.Empty;18 public System.String RemoveTablePrefix19 {20 get { return _RemoveTablePrefix; }21 set { _RemoveTablePrefix = value; }22 }23 private TableSchema _SourceTable;24 [Editor(typeof(TableEditor), typeof(TableEditor))]25 public TableSchema SourceTable26 {27 get { return _SourceTable; }28 set { _SourceTable = value; }29 }30 private DatabaseSchema _DataBase;31 [Editor(typeof(DataBaseEditor), typeof(DataBaseEditor))]32 public DatabaseSchema DataBase33 {34 get { return _DataBase; }35 set { _DataBase = value; }36 }37 private System.String _OutPutDir = string.Empty;38 [Editor(typeof(FolderEditor), typeof(FolderEditor))]39 public System.String OutPutDir40 {41 get { return _OutPutDir; }42 set { _OutPutDir = value; }43 }44 45 46 }47 }
Explain the above Code,
Private TableSchema _ SourceTable; [Editor (typeof (TableEditor), typeof (TableEditor)] public TableSchema SourceTable {get {return _ SourceTable;} set {_ SourceTable = value ;}
}
It is a field that defines a data table for the model. [Editor (typeof (TableEditor), typeof (TableEditor)] is the selector that defines calling the database table in the attribute settings. Lines 30-36 in the same Code define the attributes of a database.
Enter the following content in the Code:
1 @model DynamicCodeGenerate.myModel 2 @{ 3 foreach(var item in Model.DataBase.Tables){ 4 @:TableName:@item.Name 5 foreach(var itemcolumn in item.Columns) 6 { 7 @:Column:@itemcolumn.Name 8 } 9 }10 11 12 13 }
Explanation: @ model DynamicCodeGenerate. myModel. The code in this sentence is the same as that in Razor in asp.net mvc3. Here is the namespace and class name in the first Model definition code.
Only Model. DataBase is used in the code, so you only need to set the DataBase in the attribute Setting dialog box.
Click the DataBase field to set the attributes.
Okay. Save the template and run it. Check the result.
So easy!
: NCodeGenerate.zip
Document: Document
NCodeGenerate series of articles:
I. What is NCodeGenerate? Ii. Code Generation Tool-NCodeGenerate tutorial (1) traverse all tables in the database III. Code Generation Tool-NCodeGenerate tutorial (2) code Generation Tool-NCodeGenerate tutorial (3) generate code to a file. 5. Code Generation Tool-NCodeGenerate tutorial (4) CodeSmith template conversion 6. Code Generation Tool-NCodeGenerate tutorial (5) support for multiple databases 7. Code Generation Tool-NCodeGenerate tutorial (6) NTrace output for debugging functions 8. Code Generation Tool-NCodeGenerate tutorial (7) unveil the mystery of the debugging function 9. Code Generation Tool-NCodeGenerate tutorial (8) unveil the secrets of the Razor template engine 10. Code Generation Tool-NCodeGenerate tutorial (9) Data Type ing Map function