Can I use SQL files as an EF database migration?

I use EF6.0 and implement my db with SQLServerDatabaseProject . I want to use EF migration tools to migrate a database. but since I have my database on DbProject, I want all my migration files to be SQLFiles (not C #), so I would like to know if EF supports this function, and if not, can I write a new migration class, which saves EF functions but works this way?

Also think that I do not want EF to generate my migrations, but I would like to be able to use other migration commands, such as update-database and ...

== ADDITIONAL INFORMATION ABOUT QUESTIONS ==

I don't want C # classes to load my sql files. The sql files must be saved for up and down migration directly and processed exactly as if they were DbMigration classes. A simple Migrations dir example would be something like this:

Migrations -> up -> 201510060807125_alter-course-change-family.sql -> 201510060813136_alter-course-add-mark-column.sql -> down -> 201510060807125_alter-course-change-family.sql -> 201510060813136_alter-course-add-mark-column.sql 
+6
source share
2 answers

Just use the SqlFile extension method in the migration class:

 public partial class MyFancyMigration : DbMigration { public override void Up() { SqlFile("myUpSQLFile.sql"); } public override void Down() { SqlFile("myDownSQLFile.sql"); } } 
+5
source

Although you can use the SqlFile method, I suggest you write this using the Seed method of your Configuration.cs file in your transfer folder.

 internal sealed class Configuration : DbMigrationConfiguration<YourDbContext> { protected override void Seed(YourDbContext context) { base.Seed(context); context.Database.ExecuteSqlCommand(FileReadAllText("migration.sql")); } } 

If you write migrations in the Up method. It will be performed for each transition when updating the database, which, I think, you do not expect.

You want your scripts to be executed for each Update-Database . (Not for every migration in it)

-1
source

All Articles