Search code examples
c#entity-frameworkentity-framework-coreef-code-firstentity-framework-migrations

Custom Entity Framework Core Migration Script?


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?


Solution

  • 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.