Run custom SQL script on app start

asp.net-core c# entity-framework-core

Question

I'm using ASP.NET Core and Entity Framework Core. I need to create some views and functions programmatically on the app first start. Is there a way to run some custom SQL code while EF is being configurated?

1
1
8/9/2016 1:35:16 PM

Popular Answer

If you are using code first with entity, you can try execute your sql scripts in migration:

First you need to create a migration:

Add-Migration RunMySqlScripts

Then in the generated migration file you can write your SQL:

// PLAIN SQL
Sql("Your sql code here");

// OR FROM FILE
var sqlFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"scripts.sql"); 
Sql(File.ReadAllText(sqlFile));

Then you run:

Update-Database

This will be executed only once.

1
8/9/2016 1:45:02 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow