@@ -30,7 +30,7 @@ public CodeGenerator(ILoggerFactory loggerFactory)
3030 _synchronizer = new SourceSynchronizer ( loggerFactory ) ;
3131 }
3232
33- public GeneratorOptions Options { get ; set ; }
33+ public GeneratorOptions Options { get ; set ; } = null ! ;
3434
3535 public bool Generate ( GeneratorOptions options )
3636 {
@@ -73,13 +73,14 @@ private void GenerateQueryExtensions(EntityContext entityContext)
7373 {
7474 Options . Variables . Set ( entity ) ;
7575
76- var directory = Options . Data . Query . Directory ;
76+ var directory = Options . Data . Query . Directory ?? "Data \\ Queries" ;
7777 var file = entity . EntityClass + "Extensions.cs" ;
7878 var path = Path . Combine ( directory , file ) ;
7979
80- _logger . LogInformation ( File . Exists ( path )
81- ? "Updating query extensions class: {file}"
82- : "Creating query extensions class: {file}" , file ) ;
80+ if ( File . Exists ( path ) )
81+ _logger . LogInformation ( "Updating query extensions class: {file}" , file ) ;
82+ else
83+ _logger . LogInformation ( "Creating query extensions class: {file}" , file ) ;
8384
8485 var template = new QueryExtensionTemplate ( entity , Options ) ;
8586 template . WriteCode ( path ) ;
@@ -94,13 +95,14 @@ private void GenerateMappingClasses(EntityContext entityContext)
9495 {
9596 Options . Variables . Set ( entity ) ;
9697
97- var directory = Options . Data . Mapping . Directory ;
98+ var directory = Options . Data . Mapping . Directory ?? "Data \\ Mapping" ;
9899 var file = entity . MappingClass + ".cs" ;
99100 var path = Path . Combine ( directory , file ) ;
100101
101- _logger . LogInformation ( File . Exists ( path )
102- ? "Updating mapping class: {file}"
103- : "Creating mapping class: {file}" , file ) ;
102+ if ( File . Exists ( path ) )
103+ _logger . LogInformation ( "Updating mapping class: {file}" , file ) ;
104+ else
105+ _logger . LogInformation ( "Creating mapping class: {file}" , file ) ;
104106
105107 var template = new MappingClassTemplate ( entity , Options ) ;
106108 template . WriteCode ( path ) ;
@@ -115,13 +117,14 @@ private void GenerateEntityClasses(EntityContext entityContext)
115117 {
116118 Options . Variables . Set ( entity ) ;
117119
118- var directory = Options . Data . Entity . Directory ;
120+ var directory = Options . Data . Entity . Directory ?? "Data \\ Entities" ;
119121 var file = entity . EntityClass + ".cs" ;
120122 var path = Path . Combine ( directory , file ) ;
121123
122- _logger . LogInformation ( File . Exists ( path )
123- ? "Updating entity class: {file}"
124- : "Creating entity class: {file}" , file ) ;
124+ if ( File . Exists ( path ) )
125+ _logger . LogInformation ( "Updating entity class: {file}" , file ) ;
126+ else
127+ _logger . LogInformation ( "Creating entity class: {file}" , file ) ;
125128
126129 var template = new EntityClassTemplate ( entity , Options ) ;
127130 template . WriteCode ( path ) ;
@@ -133,13 +136,14 @@ private void GenerateEntityClasses(EntityContext entityContext)
133136 private void GenerateDataContext ( EntityContext entityContext )
134137 {
135138
136- var directory = Options . Data . Context . Directory ;
139+ var directory = Options . Data . Context . Directory ?? "Data" ;
137140 var file = entityContext . ContextClass + ".cs" ;
138141 var path = Path . Combine ( directory , file ) ;
139142
140- _logger . LogInformation ( File . Exists ( path )
141- ? "Updating data context class: {file}"
142- : "Creating data context class: {file}" , file ) ;
143+ if ( File . Exists ( path ) )
144+ _logger . LogInformation ( "Updating data context class: {file}" , file ) ;
145+ else
146+ _logger . LogInformation ( "Creating data context class: {file}" , file ) ;
143147
144148 var template = new DataContextTemplate ( entityContext , Options ) ;
145149 template . WriteCode ( path ) ;
@@ -150,7 +154,7 @@ private void GenerateModelClasses(EntityContext entityContext)
150154 {
151155 foreach ( var entity in entityContext . Entities )
152156 {
153- if ( entity . Models . Count < = 0 )
157+ if ( entity . Models . Count = = 0 )
154158 continue ;
155159
156160 Options . Variables . Set ( entity ) ;
@@ -170,14 +174,14 @@ private void GenerateModelClasses(Entity entity)
170174 {
171175 Options . Variables . Set ( model ) ;
172176
173- var directory = GetModelDirectory ( model ) ;
177+ var directory = GetModelDirectory ( model ) ?? "Data \\ Models" ;
174178 var file = model . ModelClass + ".cs" ;
175179 var path = Path . Combine ( directory , file ) ;
176180
177- _logger . LogInformation ( File . Exists ( path )
178- ? "Updating model class: {file}"
179- : "Creating model class: {file}" , file ) ;
180-
181+ if ( File . Exists ( path ) )
182+ _logger . LogInformation ( "Updating model class: {file}" , file ) ;
183+ else
184+ _logger . LogInformation ( "Creating model class: {file}" , file ) ;
181185
182186 var template = new ModelClassTemplate ( model , Options ) ;
183187 template . WriteCode ( path ) ;
@@ -187,17 +191,21 @@ private void GenerateModelClasses(Entity entity)
187191
188192 }
189193
190- private string GetModelDirectory ( Model model )
194+ private string ? GetModelDirectory ( Model model )
191195 {
192196 if ( model . ModelType == ModelType . Create )
197+ {
193198 return Options . Model . Create . Directory . HasValue ( )
194199 ? Options . Model . Create . Directory
195200 : Options . Model . Shared . Directory ;
201+ }
196202
197203 if ( model . ModelType == ModelType . Update )
204+ {
198205 return Options . Model . Update . Directory . HasValue ( )
199206 ? Options . Model . Update . Directory
200207 : Options . Model . Shared . Directory ;
208+ }
201209
202210 return Options . Model . Read . Directory . HasValue ( )
203211 ? Options . Model . Read . Directory
@@ -218,13 +226,14 @@ private void GenerateValidatorClasses(Entity entity)
218226 if ( model . ModelType == ModelType . Read )
219227 continue ;
220228
221- var directory = Options . Model . Validator . Directory ;
229+ var directory = Options . Model . Validator . Directory ?? "Data \\ Validation" ;
222230 var file = model . ValidatorClass + ".cs" ;
223231 var path = Path . Combine ( directory , file ) ;
224232
225- _logger . LogInformation ( File . Exists ( path )
226- ? "Updating validation class: {file}"
227- : "Creating validation class: {file}" , file ) ;
233+ if ( File . Exists ( path ) )
234+ _logger . LogInformation ( "Updating validation class: {file}" , file ) ;
235+ else
236+ _logger . LogInformation ( "Creating validation class: {file}" , file ) ;
228237
229238 var template = new ValidatorClassTemplate ( model , Options ) ;
230239 template . WriteCode ( path ) ;
@@ -239,13 +248,14 @@ private void GenerateMapperClass(Entity entity)
239248 if ( ! Options . Model . Mapper . Generate )
240249 return ;
241250
242- var directory = Options . Model . Mapper . Directory ;
251+ var directory = Options . Model . Mapper . Directory ?? "Data \\ Mapper" ;
243252 var file = entity . MapperClass + ".cs" ;
244253 var path = Path . Combine ( directory , file ) ;
245254
246- _logger . LogInformation ( File . Exists ( path )
247- ? "Updating object mapper class: {file}"
248- : "Creating object mapper class: {file}" , file ) ;
255+ if ( File . Exists ( path ) )
256+ _logger . LogInformation ( "Updating mapper class: {file}" , file ) ;
257+ else
258+ _logger . LogInformation ( "Creating mapper class: {file}" , file ) ;
249259
250260 var template = new MapperClassTemplate ( entity , Options ) ;
251261 template . WriteCode ( path ) ;
@@ -367,18 +377,20 @@ private DatabaseModel GetDatabaseModel(IDatabaseModelFactory factory)
367377 var database = Options . Database ;
368378
369379 var connectionString = ResolveConnectionString ( database ) ;
380+ if ( string . IsNullOrEmpty ( connectionString ) )
381+ throw new InvalidOperationException ( "Could not find connection string." ) ;
370382
371383 var options = new DatabaseModelFactoryOptions ( database . Tables , database . Schemas ) ;
372384
373385 return factory . Create ( connectionString , options ) ;
374386 }
375387
376- private string ResolveConnectionString ( DatabaseOptions database )
388+ private static string ? ResolveConnectionString ( DatabaseOptions database )
377389 {
378390 if ( database . ConnectionString . HasValue ( ) )
379391 return database . ConnectionString ;
380392
381- if ( database . UserSecretsId . HasValue ( ) )
393+ if ( database . UserSecretsId . HasValue ( ) && database . ConnectionName . HasValue ( ) )
382394 {
383395 var secretsStore = new SecretsStore ( database . UserSecretsId ) ;
384396 if ( secretsStore . ContainsKey ( database . ConnectionName ) )
@@ -434,35 +446,35 @@ private string ResolveConnectionString(DatabaseOptions database)
434446 }
435447
436448
437- private void ConfigureMySqlServices ( IServiceCollection services )
449+ private static void ConfigureMySqlServices ( IServiceCollection services )
438450 {
439451 var designTimeServices = new Pomelo . EntityFrameworkCore . MySql . Design . Internal . MySqlDesignTimeServices ( ) ;
440452 designTimeServices . ConfigureDesignTimeServices ( services ) ;
441453 services . AddEntityFrameworkMySqlNetTopologySuite ( ) ;
442454 }
443455
444- private void ConfigurePostgresServices ( IServiceCollection services )
456+ private static void ConfigurePostgresServices ( IServiceCollection services )
445457 {
446458 var designTimeServices = new Npgsql . EntityFrameworkCore . PostgreSQL . Design . Internal . NpgsqlDesignTimeServices ( ) ;
447459 designTimeServices . ConfigureDesignTimeServices ( services ) ;
448460 services . AddEntityFrameworkNpgsqlNetTopologySuite ( ) ;
449461 }
450462
451- private void ConfigureSqlServerServices ( IServiceCollection services )
463+ private static void ConfigureSqlServerServices ( IServiceCollection services )
452464 {
453465 var designTimeServices = new Microsoft . EntityFrameworkCore . SqlServer . Design . Internal . SqlServerDesignTimeServices ( ) ;
454466 designTimeServices . ConfigureDesignTimeServices ( services ) ;
455467 services . AddEntityFrameworkSqlServerNetTopologySuite ( ) ;
456468 }
457469
458- private void ConfigureSqliteServices ( IServiceCollection services )
470+ private static void ConfigureSqliteServices ( IServiceCollection services )
459471 {
460472 var designTimeServices = new Microsoft . EntityFrameworkCore . Sqlite . Design . Internal . SqliteDesignTimeServices ( ) ;
461473 designTimeServices . ConfigureDesignTimeServices ( services ) ;
462474 services . AddEntityFrameworkSqliteNetTopologySuite ( ) ;
463475 }
464476
465- private void ConfigureOracleServices ( IServiceCollection services )
477+ private static void ConfigureOracleServices ( IServiceCollection services )
466478 {
467479 var designTimeServices = new Oracle . EntityFrameworkCore . Design . Internal . OracleDesignTimeServices ( ) ;
468480 designTimeServices . ConfigureDesignTimeServices ( services ) ;
0 commit comments