6

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

2 Answers 2

5

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");
    }
}
1
  • Thanx for the answer, But I want to know if it's possible to use SQL files for migrations instead of c# classes. Oct 6, 2015 at 12:25
-1

Although you can use SqlFile method, I suggest you to write this on Seed method of your Configuration.cs file in your migration 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 Up method. It will be executed per each migration when you update the database, which I think you don't expect.

What you want is that your scripts run per each Update-Database. (Not per each migration in it)

1
  • Thanx for the answer, But I want to know if it's possible to use SQL files for migrations instead of c# classes Oct 6, 2015 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.