1
- using MsSql . ClassGenerator . Core . Common ;
1
+ using System . Reflection . Emit ;
2
+ using System . Text ;
3
+ using MsSql . ClassGenerator . Core . Common ;
2
4
using MsSql . ClassGenerator . Core . Model ;
3
5
using Serilog ;
4
6
@@ -12,7 +14,15 @@ public partial class ClassManager
12
14
/// <summary>
13
15
/// Occurs when progress was made.
14
16
/// </summary>
15
- public event EventHandler < string > ? ProgressEvent ;
17
+ public event EventHandler < string > ? ProgressEvent ;
18
+
19
+ /// <summary>
20
+ /// Gets the EF Key code.
21
+ /// </summary>
22
+ /// <remarks>
23
+ /// <b>Note</b>: The code is only generated when the option <see cref="ClassGeneratorOptions.DbModel"/> is set to <see langword="true"/>.
24
+ /// </remarks>
25
+ public EfKeyCodeResult EfKeyCode { get ; private set ; } = new ( ) ;
16
26
17
27
/// <summary>
18
28
/// Generates the classes out of the specified tables according to the specified options.
@@ -23,6 +33,8 @@ public partial class ClassManager
23
33
/// <exception cref="DirectoryNotFoundException">Will be thrown when the specified output directory doesn't exist.</exception>
24
34
public async Task GenerateClassAsync ( ClassGeneratorOptions options , List < TableEntry > tables )
25
35
{
36
+ EfKeyCode = new EfKeyCodeResult ( ) ;
37
+
26
38
// Step 0: Check the options.
27
39
if ( ! Directory . Exists ( options . Output ) )
28
40
throw new DirectoryNotFoundException ( $ "The specified output ({ options . Output } ) folder doesn't exist.") ;
@@ -40,6 +52,10 @@ public async Task GenerateClassAsync(ClassGeneratorOptions options, List<TableEn
40
52
Log . Debug ( "Generate class for table '{name}'..." , table . Name ) ;
41
53
await GenerateClassAsync ( options , table ) ;
42
54
}
55
+
56
+ // Generate the EF Key code
57
+ if ( options . DbModel )
58
+ GenerateEfKeyCode ( tables ) ;
43
59
}
44
60
45
61
/// <summary>
@@ -248,4 +264,70 @@ private static string GetPropertyAttributes(ClassGeneratorOptions options, Colum
248
264
249
265
return string . Join ( Environment . NewLine , attributes . OrderBy ( o => o . Key ) . Select ( s => s . Value ) ) ;
250
266
}
267
+
268
+ /// <summary>
269
+ /// Generates the EF Key code.
270
+ /// </summary>
271
+ /// <param name="tables">The list with the tables.</param>
272
+ private void GenerateEfKeyCode ( List < TableEntry > tables )
273
+ {
274
+ // Get all tables which contains more than one key column
275
+ var tmpTables = tables . Where ( w => w . Columns . Count ( c => c . IsPrimaryKey ) > 1 ) . ToList ( ) ;
276
+ if ( tmpTables . Count == 0 )
277
+ return ;
278
+
279
+ var sb = PrepareStringBuilder ( ) ;
280
+ var count = 1 ;
281
+ foreach ( var tableEntry in tmpTables )
282
+ {
283
+ // Add the entity
284
+ sb . AppendLine ( $ "{ Tab } modelBuilder.Entity<{ tableEntry . ClassName } >().HasKey(k => new")
285
+ . AppendLine ( $ "{ Tab } {{") ;
286
+
287
+ // Get the key columns
288
+ var columnCount = 1 ;
289
+ var columns = tableEntry . Columns . Where ( w => w . IsPrimaryKey ) . ToList ( ) ;
290
+ foreach ( var columnEntry in columns )
291
+ {
292
+ var comma = columnCount ++ != columns . Count ? "," : string . Empty ;
293
+
294
+ sb . AppendLine ( $ "{ Tab } { Tab } k.{ columnEntry . PropertyName } { comma } ") ;
295
+ }
296
+
297
+ // Add the closing brackets
298
+ sb . AppendLine ( $ "{ Tab } }});") ;
299
+
300
+ if ( count ++ != tmpTables . Count )
301
+ sb . AppendLine ( ) ; // Spacer
302
+
303
+ }
304
+
305
+ EfKeyCode = new EfKeyCodeResult
306
+ {
307
+ Code = FinalizeStringBuilder ( ) ,
308
+ TableCount = tmpTables . Count
309
+ } ;
310
+
311
+ return ;
312
+
313
+ StringBuilder PrepareStringBuilder ( )
314
+ {
315
+ var stringBuilder = new StringBuilder ( )
316
+ . AppendLine ( "/// <inheritdoc />" )
317
+ . AppendLine ( "protected override void OnModelCreating(ModelBuilder modelBuilder)" )
318
+ . AppendLine ( "{" ) ;
319
+
320
+ return stringBuilder ;
321
+ }
322
+
323
+ // Adds the final code
324
+ string FinalizeStringBuilder ( )
325
+ {
326
+ sb . AppendLine ( "}" ) ;
327
+
328
+ return sb . ToString ( ) ;
329
+ }
330
+ }
331
+
332
+
251
333
}
0 commit comments