Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to use sql files as EF database migrations?

I'm using EF6.0 and implementing my db with SQLServerDatabaseProject. I want to use the EF Migration tools for Database migration. 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 feature and if not, is it possible to write a new Migration class which keeps the EF features but works this way?

Please also consider that I don't want EF to generate my migrations but I would like to be able to use other migration commands such as update-database and ...

==MORE DETAILS ABOUT THE QUESTION==

I don't want to have c# classes load my sql files. The sql files must be saved for up and down migrations directly and be treated exactly as if they are the DbMigration classes. A simple example of Migrations dir 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
like image 715
Hossein Shahdoost Avatar asked Oct 06 '15 09:10

Hossein Shahdoost


People also ask

Which type of file is used in database migration?

Database Migration Scripts Are Tool-Dependent This time, the file is in XML format. In actual fact, Liquibase can produce migration changes (or changesets, as they like to call them) in multiple formats such as XML and JSON.

What is SQL migration file?

A SQL migration script is similar to a SQL build script, except that it changes a database from one version to another, rather than builds it from scratch. Although they're simple in essence, it is worth knowing how to use them effectively for stress-free database updates and deployments.

Can we run SQL script using Code First migrations?

Code First Migrations will run the migration pipeline but instead of actually applying the changes it will write them out to a . sql file for you. Once the script is generated, it is opened for you in Visual Studio, ready for you to view or save.


1 Answers

Simply in the migration class use SqlFile extension method:

public partial class MyFancyMigration : DbMigration
{
    public override void Up()
    {
        SqlFile("myUpSQLFile.sql");
    }

    public override void Down()
    {
        SqlFile("myDownSQLFile.sql");
    }
}
like image 105
Sam FarajpourGhamari Avatar answered Oct 12 '22 23:10

Sam FarajpourGhamari