11using System . Diagnostics . CodeAnalysis ;
22using System . Globalization ;
3+ using System . Text . Json ;
34using Microsoft . EntityFrameworkCore . Infrastructure ;
5+ using Microsoft . EntityFrameworkCore . Metadata . Conventions . Infrastructure ;
46using Microsoft . EntityFrameworkCore . Migrations ;
57using Microsoft . EntityFrameworkCore . Query ;
68using Microsoft . EntityFrameworkCore . SqlServer . Query . Internal ;
79using Microsoft . Extensions . DependencyInjection ;
810using Microsoft . Extensions . DependencyInjection . Extensions ;
911using Thinktecture . EntityFrameworkCore . BulkOperations ;
1012using Thinktecture . EntityFrameworkCore . Migrations ;
13+ using Thinktecture . EntityFrameworkCore . Parameters ;
1114using Thinktecture . EntityFrameworkCore . Query ;
1215using Thinktecture . EntityFrameworkCore . TempTables ;
1316
@@ -89,6 +92,9 @@ public bool AddCustomQuerySqlGeneratorFactory
8992 /// </summary>
9093 public bool AddBulkOperationSupport { get ; set ; }
9194
95+ private JsonSerializerOptions ? _collectionParameterJsonSerializerOptions ;
96+ private bool _addCollectionParameterSupport ;
97+
9298 /// <summary>
9399 /// Changes the implementation of <see cref="IMigrationsSqlGenerator"/> to <see cref="ThinktectureSqlServerMigrationsSqlGenerator"/>.
94100 /// </summary>
@@ -133,6 +139,14 @@ public void ApplyServices(IServiceCollection services)
133139 services . TryAddScoped < ITruncateTableExecutor > ( provider => provider . GetRequiredService < SqlServerBulkOperationExecutor > ( ) ) ;
134140 }
135141
142+ if ( _addCollectionParameterSupport )
143+ {
144+ var jsonSerializerOptions = _collectionParameterJsonSerializerOptions ?? new JsonSerializerOptions ( ) ;
145+
146+ services . AddSingleton < ICollectionParameterFactory > ( _ => new SqlServerCollectionParameterFactory ( jsonSerializerOptions ) ) ;
147+ services . Add < IConventionSetPlugin , SqlServerCollectionParameterConventionSetPlugin > ( GetLifetime < IConventionSetPlugin > ( ) ) ;
148+ }
149+
136150 if ( UseThinktectureSqlServerMigrationsSqlGenerator )
137151 AddWithCheck < IMigrationsSqlGenerator , ThinktectureSqlServerMigrationsSqlGenerator , SqlServerMigrationsSqlGenerator > ( services ) ;
138152
@@ -163,6 +177,15 @@ public void Register(Type serviceType, object implementationInstance)
163177 _relationalOptions . Register ( serviceType , implementationInstance ) ;
164178 }
165179
180+ /// <summary>
181+ /// Enables and disables support for queryable parameters.
182+ /// </summary>
183+ public void AddCollectionParameterSupport ( bool addCollectionParameterSupport , JsonSerializerOptions ? jsonSerializerOptions )
184+ {
185+ _addCollectionParameterSupport = addCollectionParameterSupport ;
186+ _collectionParameterJsonSerializerOptions = jsonSerializerOptions ;
187+ }
188+
166189 /// <inheritdoc />
167190 public void Validate ( IDbContextOptions options )
168191 {
@@ -181,6 +204,7 @@ private class SqlServerDbContextOptionsExtensionInfo : DbContextOptionsExtension
181204 'Custom QuerySqlGeneratorFactory'={ _extension . AddCustomQuerySqlGeneratorFactory } ,
182205 'Custom RelationalParameterBasedSqlProcessorFactory'={ _extension . AddCustomRelationalParameterBasedSqlProcessorFactory } ,
183206 'BulkOperationSupport'={ _extension . AddBulkOperationSupport } ,
207+ 'CollectionParameterSupport'={ _extension . _addCollectionParameterSupport } ,
184208 'TenantDatabaseSupport'={ _extension . AddTenantDatabaseSupport } ,
185209 'TableHintSupport'={ _extension . AddTableHintSupport } ,
186210 'UseThinktectureSqlServerMigrationsSqlGenerator'={ _extension . UseThinktectureSqlServerMigrationsSqlGenerator }
@@ -196,13 +220,19 @@ public SqlServerDbContextOptionsExtensionInfo(SqlServerDbContextOptionsExtension
196220 /// <inheritdoc />
197221 public override int GetServiceProviderHashCode ( )
198222 {
199- return HashCode . Combine ( _extension . AddCustomQueryableMethodTranslatingExpressionVisitorFactory ,
200- _extension . AddCustomQuerySqlGeneratorFactory ,
201- _extension . AddCustomRelationalParameterBasedSqlProcessorFactory ,
202- _extension . AddBulkOperationSupport ,
203- _extension . AddTenantDatabaseSupport ,
204- _extension . AddTableHintSupport ,
205- _extension . UseThinktectureSqlServerMigrationsSqlGenerator ) ;
223+ var hashCode = new HashCode ( ) ;
224+
225+ hashCode . Add ( _extension . AddCustomQueryableMethodTranslatingExpressionVisitorFactory ) ;
226+ hashCode . Add ( _extension . AddCustomQuerySqlGeneratorFactory ) ;
227+ hashCode . Add ( _extension . AddCustomRelationalParameterBasedSqlProcessorFactory ) ;
228+ hashCode . Add ( _extension . AddBulkOperationSupport ) ;
229+ hashCode . Add ( _extension . _addCollectionParameterSupport ) ;
230+ hashCode . Add ( _extension . _collectionParameterJsonSerializerOptions ) ;
231+ hashCode . Add ( _extension . AddTenantDatabaseSupport ) ;
232+ hashCode . Add ( _extension . AddTableHintSupport ) ;
233+ hashCode . Add ( _extension . UseThinktectureSqlServerMigrationsSqlGenerator ) ;
234+
235+ return hashCode . ToHashCode ( ) ;
206236 }
207237
208238 /// <inheritdoc />
@@ -213,6 +243,8 @@ public override bool ShouldUseSameServiceProvider(DbContextOptionsExtensionInfo
213243 && _extension . AddCustomQuerySqlGeneratorFactory == otherSqlServerInfo . _extension . AddCustomQuerySqlGeneratorFactory
214244 && _extension . AddCustomRelationalParameterBasedSqlProcessorFactory == otherSqlServerInfo . _extension . AddCustomRelationalParameterBasedSqlProcessorFactory
215245 && _extension . AddBulkOperationSupport == otherSqlServerInfo . _extension . AddBulkOperationSupport
246+ && _extension . _addCollectionParameterSupport == otherSqlServerInfo . _extension . _addCollectionParameterSupport
247+ && _extension . _collectionParameterJsonSerializerOptions == otherSqlServerInfo . _extension . _collectionParameterJsonSerializerOptions
216248 && _extension . AddTenantDatabaseSupport == otherSqlServerInfo . _extension . AddTenantDatabaseSupport
217249 && _extension . AddTableHintSupport == otherSqlServerInfo . _extension . AddTableHintSupport
218250 && _extension . UseThinktectureSqlServerMigrationsSqlGenerator == otherSqlServerInfo . _extension . UseThinktectureSqlServerMigrationsSqlGenerator ;
@@ -225,6 +257,7 @@ public override void PopulateDebugInfo(IDictionary<string, string> debugInfo)
225257 debugInfo [ "Thinktecture:CustomQuerySqlGeneratorFactory" ] = _extension . AddCustomQuerySqlGeneratorFactory . ToString ( CultureInfo . InvariantCulture ) ;
226258 debugInfo [ "Thinktecture:CustomRelationalParameterBasedSqlProcessorFactory" ] = _extension . AddCustomRelationalParameterBasedSqlProcessorFactory . ToString ( CultureInfo . InvariantCulture ) ;
227259 debugInfo [ "Thinktecture:BulkOperationSupport" ] = _extension . AddBulkOperationSupport . ToString ( CultureInfo . InvariantCulture ) ;
260+ debugInfo [ "Thinktecture:CollectionParameterSupport" ] = _extension . _addCollectionParameterSupport . ToString ( CultureInfo . InvariantCulture ) ;
228261 debugInfo [ "Thinktecture:TenantDatabaseSupport" ] = _extension . AddTenantDatabaseSupport . ToString ( CultureInfo . InvariantCulture ) ;
229262 debugInfo [ "Thinktecture:TableHintSupport" ] = _extension . AddTableHintSupport . ToString ( CultureInfo . InvariantCulture ) ;
230263 debugInfo [ "Thinktecture:UseThinktectureSqlServerMigrationsSqlGenerator" ] = _extension . UseThinktectureSqlServerMigrationsSqlGenerator . ToString ( CultureInfo . InvariantCulture ) ;
0 commit comments