2

I need to make a "full-text indexed" but I am using ef core 2.1 code first migration, but Full Indexes do not have first class support in core.

How can I write my own migration that would be applied while the other generated migrations are being applied?

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
chobo2
  • 83,322
  • 195
  • 530
  • 832
  • `migrationBuilder.Sql` method is your friend. For more info, see [Migrations](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/) and [API Reference](https://learn.microsoft.com/en-us/dotnet/api/?view=efcore-2.1). – Ivan Stoev Jun 26 '18 at 17:38
  • So, I generate the migration file then go in and manually change it or do I make a another file? – chobo2 Jun 26 '18 at 17:40
  • Manually change it. Your use-case with Full-Text Search is even mentioned in the [Empty Migrations](https://learn.microsoft.com/en-us/ef/core/managing-schemas/migrations/#empty-migrations) part of the first documentation link. – Ivan Stoev Jun 26 '18 at 17:42
  • This was answered in your Github issue [Full Text Search in EF Core 2.1](https://github.com/aspnet/EntityFrameworkCore/issues/12461) and an example was provided your in SO question [How to use FreeText in EF core 2.1](https://stackoverflow.com/questions/51047514/how-to-use-freetext-in-ef-core-2-1). – Mark G Jun 27 '18 at 01:26

1 Answers1

5

You have to create empty migration and then in Up method you can write your SQL query.

  1. Create Empty Migration by running Add-Migration command in Package Manager Console.
  2. Add the SQL function in Up method like the following way.

public partial class your_migration_name : Migration {

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.Sql(@"<YourQuery>");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.Sql(@"Drop <YourQuery>");
}
}
  1. Run Update-Database in Package Manager Console.
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197