22// The .NET Foundation licenses this file to you under the MIT license.
33
44using Microsoft . Data . SqlClient ;
5+ using Microsoft . EntityFrameworkCore . SqlServer . Metadata . Internal ;
56
67// ReSharper disable InconsistentNaming
78namespace Microsoft . EntityFrameworkCore . Migrations ;
@@ -21,6 +22,25 @@ [ProductVersion] nvarchar(32) NOT NULL,
2122 CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
2223);
2324
25+ """ , sql , ignoreLineEndingDifferences : true ) ;
26+ }
27+
28+ [ ConditionalFact ]
29+ public void GetCreateScript_works_with_full_text_catalog ( )
30+ {
31+ // Inject a model finalizing convention that adds a full-text catalog to the model, simulating a scenario where
32+ // provider conventions add database-level annotations. Without filtering, the history table creation script would
33+ // include CREATE FULLTEXT CATALOG.
34+ var sql = CreateHistoryRepository ( addFullTextCatalogConvention : true ) . GetCreateScript ( ) ;
35+
36+ Assert . Equal (
37+ """
38+ CREATE TABLE [__EFMigrationsHistory] (
39+ [MigrationId] nvarchar(150) NOT NULL,
40+ [ProductVersion] nvarchar(32) NOT NULL,
41+ CONSTRAINT [PK___EFMigrationsHistory] PRIMARY KEY ([MigrationId])
42+ );
43+
2444""" , sql , ignoreLineEndingDifferences : true ) ;
2545 }
2646
@@ -149,17 +169,28 @@ public void GetEndIfScript_works()
149169""" , sql , ignoreLineEndingDifferences : true ) ;
150170 }
151171
152- private static IHistoryRepository CreateHistoryRepository ( string schema = null )
153- => new TestDbContext (
172+ private static IHistoryRepository CreateHistoryRepository (
173+ string schema = null ,
174+ Action < ModelBuilder > configureModel = null ,
175+ bool addFullTextCatalogConvention = false )
176+ {
177+ var serviceProvider = addFullTextCatalogConvention
178+ ? SqlServerTestHelpers . Instance . CreateServiceProvider (
179+ new ServiceCollection ( ) . AddSingleton < IConventionSetPlugin , FullTextCatalogConventionPlugin > ( ) )
180+ : SqlServerTestHelpers . Instance . CreateServiceProvider ( ) ;
181+
182+ return new TestDbContext (
154183 new DbContextOptionsBuilder ( )
155- . UseInternalServiceProvider ( SqlServerTestHelpers . Instance . CreateServiceProvider ( ) )
184+ . UseInternalServiceProvider ( serviceProvider )
156185 . UseSqlServer (
157186 new SqlConnection ( "Database=DummyDatabase" ) ,
158187 b => b . MigrationsHistoryTable ( HistoryRepository . DefaultTableName , schema ) )
159- . Options )
188+ . Options ,
189+ configureModel )
160190 . GetService < IHistoryRepository > ( ) ;
191+ }
161192
162- private class TestDbContext ( DbContextOptions options ) : DbContext ( options )
193+ private class TestDbContext ( DbContextOptions options , Action < ModelBuilder > configureModel = null ) : DbContext ( options )
163194 {
164195 public DbSet < Blog > Blogs { get ; set ; }
165196
@@ -169,9 +200,35 @@ public IQueryable<TableFunction> TableFunction()
169200
170201 protected override void OnModelCreating ( ModelBuilder modelBuilder )
171202 {
203+ configureModel ? . Invoke ( modelBuilder ) ;
204+ }
205+ }
206+
207+ /// <summary>
208+ /// A convention plugin that adds a full-text catalog annotation to the model, simulating what a provider convention
209+ /// might do. This allows testing that <see cref="SqlServerHistoryRepository.GetCreateCommands" /> properly filters
210+ /// out the full-text catalog from the history table creation script.
211+ /// </summary>
212+ private class FullTextCatalogConventionPlugin : IConventionSetPlugin
213+ {
214+ public ConventionSet ModifyConventions ( ConventionSet conventionSet )
215+ {
216+ conventionSet . ModelFinalizingConventions . Add ( new FullTextCatalogAddingConvention ( ) ) ;
217+ return conventionSet ;
172218 }
173219 }
174220
221+ private class FullTextCatalogAddingConvention : IModelFinalizingConvention
222+ {
223+ #pragma warning disable EF1001 // Internal EF Core API usage.
224+ public void ProcessModelFinalizing (
225+ IConventionModelBuilder modelBuilder ,
226+ IConventionContext < IConventionModelBuilder > context )
227+ => SqlServerFullTextCatalog . AddFullTextCatalog (
228+ ( IMutableModel ) modelBuilder . Metadata , "TestCatalog" , ConfigurationSource . Convention ) ;
229+ #pragma warning restore EF1001 // Internal EF Core API usage.
230+ }
231+
175232 private class Blog
176233 {
177234 public int Id { get ; set ; }
0 commit comments