Efcore: I want implement code in Migration class and return SQL code for applying !

Created on 21 Jul 2018  路  1Comment  路  Source: dotnet/efcore

Hie fellows!
I'm still trying to implement custom Migrator!

1.I have question !
For example I have this migration, it s possible to generate SQL code Migration class ?
My code that generates SQL CODE for aplying !
I want implement code in Migration class and return SQL code for applying !
And i wanna generate code in runtime !
tables,fields ! And apply them in runtime !

2.Its possible to realize GenerateScript method for Migration Class ?
```
var str = "";
using (var db = _context)
{
var migrator = db.GetService();
str =migrator.GenerateScript(fromMigration: "20180714091307_aa1", toMigration:"20180714101619_aa2");
}


//20180714101619_aa2.cs
using Microsoft.EntityFrameworkCore.Migrations;
using System;

namespace Test_Migration.Migrations
{
public partial class aa2 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
Random rand = new Random();
int temp;
temp = rand.Next(100);
// My custom code !
migrationBuilder.AddColumn(
name: "DIAS_COLUMN"+ temp.ToString(),
table: "Books",
nullable: true);

    }
}

}
```

closed-question customer-reported

Most helpful comment

Tnks @bricelam
Actually its possible with your post
https://github.com/aspnet/EntityFrameworkCore/issues/6214#issuecomment-332980580

            var db = _context;
            aa2 Migra = new aa2();
            var sqlGenerator = db.GetService<IMigrationsSqlGenerator>();
            var commands = sqlGenerator.Generate(Migra.UpOperations, null);
            var executor = db.GetService<IMigrationCommandExecutor>();
            executor.ExecuteNonQuery(commands, db.GetService<IRelationalConnection>());

Deep diving in ef core )))

>All comments

Tnks @bricelam
Actually its possible with your post
https://github.com/aspnet/EntityFrameworkCore/issues/6214#issuecomment-332980580

            var db = _context;
            aa2 Migra = new aa2();
            var sqlGenerator = db.GetService<IMigrationsSqlGenerator>();
            var commands = sqlGenerator.Generate(Migra.UpOperations, null);
            var executor = db.GetService<IMigrationCommandExecutor>();
            executor.ExecuteNonQuery(commands, db.GetService<IRelationalConnection>());

Deep diving in ef core )))

Was this page helpful?
0 / 5 - 0 ratings