@@ -34,13 +34,14 @@ public TablesDal(string projectName, string modelpath, string schemaPath, string
3434 this . dalpath = dalpath ;
3535 this . schemaName = schemaName ;
3636 this . table = table ;
37- Get_Fields ( ) ;
38- Get_Primarykey ( fieldList [ 0 ] . Oid ) ;
39- Get_Constraint ( ) ;
4037 }
4138
4239 public void Create ( )
4340 {
41+ Get_Fields ( ) ;
42+ Get_Primarykey ( fieldList [ 0 ] . Oid ) ;
43+ Get_Constraint ( ) ;
44+
4445 CreateModel ( ) ;
4546 CreateSchema ( ) ;
4647 CreateDal ( ) ;
@@ -62,7 +63,7 @@ public void CreateModel()
6263 writer . WriteLine ( ) ;
6364 writer . WriteLine ( $ "namespace { projectName } .Model") ;
6465 writer . WriteLine ( "{" ) ;
65- writer . WriteLine ( $ "\t [EntityMapping(name: \" { this . table . name } \" , Schema = \" { this . schemaName } \" )]") ;
66+ writer . WriteLine ( $ "\t [EntityMapping(name: \" { this . table . Name } \" , Schema = \" { this . schemaName } \" )]") ;
6667 writer . WriteLine ( $ "\t public partial class { _classname } ") ;
6768 writer . WriteLine ( "\t {" ) ;
6869
@@ -78,24 +79,24 @@ public void CreateModel()
7879 writer . WriteLine ( $ "\t \t public { _type } { item . Field . ToUpperPascal ( ) } {{ get; set; }}") ;
7980 writer . WriteLine ( ) ;
8081 }
81- if ( this . table . type == "table" )
82+ if ( this . table . Type == "table" )
8283 {
8384 string dalPath = $ "{ projectName } .DAL.";
8485 Hashtable ht = new Hashtable ( ) ;
8586 foreach ( var item in consList )
8687 {
87- string f_dalName = CreateName ( item . nspname , item . table_name ) ;
88- string pname = $ "{ item . table_name . ToUpperPascal ( ) } ";
88+ string f_dalName = CreateName ( item . NspName , item . TablaName ) ;
89+ string pname = $ "{ item . TablaName . ToUpperPascal ( ) } ";
8990 string propertyName = f_dalName ;
9091 if ( ht . ContainsKey ( propertyName ) || _classname == propertyName )
9192 {
92- propertyName += "By" + item . conname . ToUpperPascal ( ) ;
93+ propertyName += "By" + item . ConlumnName . ToUpperPascal ( ) ;
9394 }
9495
9596
9697 string tmp_var = propertyName . ToLowerPascal ( ) ;
9798 writer . WriteLine ( $ "\t \t private { f_dalName } Model { tmp_var } = null;") ;
98- writer . WriteLine ( $ "\t \t [ForeignKeyMapping(name: \" { item . conname } \" ), JsonIgnore] public { f_dalName } Model { propertyName } {{ get {{ if ({ tmp_var } == null) { tmp_var } = { dalPath } { f_dalName } .Context.Where(f => f.{ item . ref_column . ToUpperPascal ( ) } == this.{ item . conname . ToUpperPascal ( ) } ).ToOne(); return { tmp_var } ; }} }}") ;
99+ writer . WriteLine ( $ "\t \t [ForeignKeyMapping(name: \" { item . ConlumnName } \" ), JsonIgnore] public { f_dalName } Model { propertyName } {{ get {{ if ({ tmp_var } == null) { tmp_var } = { dalPath } { f_dalName } .Context.Where(f => f.{ item . RefColumn . ToUpperPascal ( ) } == this.{ item . ConlumnName . ToUpperPascal ( ) } ).ToOne(); return { tmp_var } ; }} }}") ;
99100 writer . WriteLine ( ) ;
100101 ht . Add ( propertyName , "" ) ;
101102 }
@@ -157,9 +158,15 @@ protected void CreateSchema()
157158 for ( int i = 0 ; i < fieldList . Count ; i ++ )
158159 {
159160 var fi = fieldList [ i ] ;
160- string specificType = GetspecificType ( fi ) ;
161+ string specificType = GetSpecificType ( fi ) ;
161162 string ap = fi . Is_array ? " | NpgsqlDbType.Array" : "" ;
162- var line = $ "{{\" { fi . Field } \" ,new SchemaModel{{ FieldName=\" { fi . Field } \" , DbType= NpgsqlDbType.{ fi . PgDbType } { ap } , Size={ fi . Length } , SpecificType={ specificType } }} }}";
163+ var pk = pkList . FirstOrDefault ( f => f . Field == fi . Field ) != null ;
164+ var primarykey = "" ;
165+ if ( pk )
166+ {
167+ primarykey = " ,Primarykey = true" ;
168+ }
169+ var line = $ "{{\" { fi . Field } \" , new SchemaModel{{ FieldName = \" { fi . Field } \" , DbType = NpgsqlDbType.{ fi . PgDbType } { ap } , Size = { fi . Length } , SpecificType = { specificType } { primarykey } }} }}";
163170 writer . WriteLine ( "\t \t \t \t " + line + ( i + 1 == fieldList . Count ? "" : "," ) ) ;
164171 }
165172 writer . WriteLine ( "\t \t \t };" ) ;
@@ -187,7 +194,7 @@ private string CreateName(string schema, string tableName, string separator = ""
187194
188195 private string CreateName ( )
189196 {
190- return CreateName ( this . schemaName , this . table . name ) ;
197+ return CreateName ( this . schemaName , this . table . Name ) ;
191198 }
192199
193200 protected void CreateDal ( )
@@ -232,7 +239,7 @@ protected void CreateDal()
232239 }
233240 }
234241
235- if ( this . table . type == "table" )
242+ if ( this . table . Type == "table" )
236243 {
237244 writer . WriteLine ( ) ;
238245 Insert_Generator ( writer , _model_classname , _classname ) ;
@@ -320,25 +327,23 @@ void CreateConstructor(string paramString, string onChange = null)
320327
321328 writer . WriteLine ( $ "\t \t \t public new { updateName } Where(Expression<Func<{ class_model . ToUpperPascal ( ) } , bool>> predicate)") ;
322329 writer . WriteLine ( "\t \t \t {" ) ;
323- writer . WriteLine ( $ "\t \t \t \t base .Where(predicate);") ;
324- writer . WriteLine ( $ "\t \t \t \t return this;") ;
330+ writer . WriteLine ( $ "\t \t \t \t base .Where(predicate);") ;
331+ writer . WriteLine ( $ "\t \t \t \t return this;") ;
325332 writer . WriteLine ( "\t \t \t }" ) ;
326- writer . WriteLine ( ) ;
327333
328334 writer . WriteLine ( $ "\t \t \t public new { updateName } Where(string formatCommad, params object[] pValue)") ;
329335 writer . WriteLine ( "\t \t \t {" ) ;
330- writer . WriteLine ( $ "\t \t \t \t base .Where(formatCommad,pValue);") ;
331- writer . WriteLine ( $ "\t \t \t \t return this;") ;
336+ writer . WriteLine ( $ "\t \t \t \t base .Where(formatCommad,pValue);") ;
337+ writer . WriteLine ( $ "\t \t \t \t return this;") ;
332338 writer . WriteLine ( "\t \t \t }" ) ;
333- writer . WriteLine ( ) ;
334339
335340 foreach ( var item in fieldList )
336341 {
337342 if ( item . Is_identity ) continue ;
338343
339344 writer . WriteLine ( $ "\t \t \t public { updateName } Set{ item . Field . ToUpperPascal ( ) } ({ item . RelType } { item . Field } )") ;
340345 writer . WriteLine ( "\t \t \t {" ) ;
341- string specificType = GetspecificType ( item ) ;
346+ string specificType = GetSpecificType ( item ) ;
342347 string ap = item . Is_array ? " | NpgsqlDbType.Array" : "" ;
343348 writer . WriteLine ( $ "\t \t \t \t base.SetField(\" { item . Field } \" , NpgsqlDbType.{ item . PgDbType } { ap } , { item . Field } , { item . Length } , { specificType } );") ;
344349 writer . WriteLine ( $ "\t \t \t \t return this;") ;
@@ -358,7 +363,6 @@ void CreateConstructor(string paramString, string onChange = null)
358363 writer . WriteLine ( $ "\t \t \t \t return this;") ;
359364 writer . WriteLine ( "\t \t \t }" ) ;
360365 }
361- writer . WriteLine ( ) ;
362366 }
363367 writer . WriteLine ( "\t \t }" ) ;
364368 }
@@ -405,7 +409,7 @@ from pg_class a
405409 left join pg_type e2 on e2.oid=e.typelem
406410 inner join information_schema.columns f on f.table_schema = b.nspname and f.table_name=a.relname and column_name = c.attname
407411 WHERE b.nspname='{0}' and a.relname='{1}';" ;
408- _sqltext = string . Format ( _sqltext , this . schemaName , this . table . name ) ;
412+ _sqltext = string . Format ( _sqltext , this . schemaName , this . table . Name ) ;
409413
410414
411415 PgSqlHelper . ExecuteDataReader ( dr =>
@@ -448,7 +452,7 @@ from pg_class a
448452 } , CommandType . Text , _sqltext ) ;
449453 }
450454
451- private string GetspecificType ( FieldInfo fi )
455+ private string GetSpecificType ( FieldInfo fi )
452456 {
453457 string specificType = "null" ;
454458 if ( fi . Data_Type == "e" )
@@ -462,13 +466,13 @@ protected void Get_Primarykey(int oid)
462466 string _sqltext = $@ "SELECT b.attname, format_type(b.atttypid, b.atttypmod) AS data_type
463467FROM pg_index a
464468INNER JOIN pg_attribute b ON b.attrelid = a.indrelid AND b.attnum = ANY(a.indkey)
465- WHERE a.indrelid = '{ schemaName } .{ table . name } '::regclass AND a.indisprimary;
469+ WHERE a.indrelid = '{ schemaName } .{ table . Name } '::regclass AND a.indisprimary;
466470" ;
467471 PgSqlHelper . ExecuteDataReader ( dr =>
468472 {
469473 PrimarykeyInfo pk = new PrimarykeyInfo ( ) ;
470474 pk . Field = dr [ "attname" ] . ToString ( ) ;
471- pk . Typname = dr [ "data_type" ] . ToString ( ) ;
475+ pk . TypeName = dr [ "data_type" ] . ToString ( ) ;
472476 pkList . Add ( pk ) ;
473477 } , CommandType . Text , _sqltext ) ;
474478 }
@@ -488,7 +492,7 @@ WHERE conrelid in
488492SELECT a.oid FROM pg_class a
489493inner join pg_namespace b on a.relnamespace=b.oid
490494WHERE b.nspname='{0}' and a.relname='{1}');"
491- , this . schemaName , this . table . name ) ;
495+ , this . schemaName , this . table . Name ) ;
492496
493497
494498 PgSqlHelper . ExecuteDataReader ( dr =>
@@ -500,11 +504,11 @@ SELECT a.oid FROM pg_class a
500504 string nspname = dr [ "nspname" ] . ToString ( ) ;
501505 consList . Add ( new ConstraintInfo ( )
502506 {
503- conname = conname ,
504- contype = contype ,
505- ref_column = ref_column ,
506- table_name = relname ,
507- nspname = nspname
507+ ConlumnName = conname ,
508+ ConlumnType = contype ,
509+ RefColumn = ref_column ,
510+ TablaName = relname ,
511+ NspName = nspname
508512 } ) ;
509513 } , CommandType . Text , _sqltext ) ;
510514 }
0 commit comments