0

How do I run a SQL command during seeding on ASP.NET Boilerplate?

I need to run a complicated query so I think it will be easier to use a stored procedure to offload the computing onto SQL Server.

I've successfully created a custom repository which injects ActiveTransactionProvider and uses ADO.NET from the DbContext to run the query.

Now I want to check if the stored procedure exists and if not create it in the seeding when the backend starts up.

I tried to use ADO.NET from the DbContext passed SeedHelper but I get an exception that I need a transaction even though the transaction scope has been set to suppressed in the helper. I don't see where I can inject the active transaction provider during seeding so I'm stuck.

Andre Odendaal
  • 557
  • 6
  • 19

1 Answers1

1

You need to use code first migrations to alter database schema (including creating a SP). Just use add-migration command then execute SQL inside it (like Code First Migrations and Stored Procedures).

Seed is just for adding/updating existing entities. It just runs on application startups. EF Core does not have native seed mechanism yet (as I know).

hikalkan
  • 2,234
  • 15
  • 17