9

I want to create stored procedure using Migration Builder but there is no

CreateStoredProcedure

method in Migration class like this.

public override void Up() 
{
  CreateStoredProcedure(
    "MyStoredProcedure",
    p => new
    {
        id = p.Int()
    },
    @"SELECT some-data FROM my-table WHERE id = @id"
  );
}

public override void Down() 
{
  DropStoredProcedure("MyStoredProcedure");
}

How to Create Stored procedure in Migration using Entity Framework Core?

Radenko Zec
  • 7,659
  • 6
  • 35
  • 39
  • It is not a feature yet. So ado.net is an option http://stackoverflow.com/questions/28599404/how-to-run-stored-procedures-in-entity-framework-7 – Aravind Jul 06 '16 at 11:58
  • 1
    I know how to call stored procedure from Entity Framework. I want to create stored procedure in Database using Entity Framework Migrations. – Radenko Zec Jul 06 '16 at 11:59

1 Answers1

11

Use can use the migrationBuilder.Sql() method to execute arbitrary SQL in migrations with EF Core

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • 3
    Strangely enough if you're migration only contains stored procs (no model changes) then the migration history table will not be updated. This causes issues with any following migrations. To resolve this I just insert a migration history entry at the same time as the stored proc. – Svenkle Dec 27 '17 at 11:07
  • 4
    Just FYI, I did not have the above issue with the migration history entry not being entered when only creating stored procs. This is with EF Core 2.2. – CapnChaos Mar 15 '19 at 13:33
  • I'm having the issue that if i try this it will break since i'm not allowed to create a stored procedure inside a stored procedure.... – Vincent Nov 10 '20 at 08:01