Is it possible and how create a trigger with Entity Framework Core
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"create trigger .....");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"drop trigger <triggerName>");
}
Laraue.EfCoreTriggers package for creating SQL triggers through fluent syntax which allows to execute insert, update, upsert, delete, insert if not exists statements after trigger has worked like this
modelBuilder.Entity<Transaction>()
.AfterInsert(trigger => trigger
.Action(triggerAction => triggerAction
.Upsert(transaction => new { transaction.UserId },
insertedTransaction => new UserBalance { UserId = transaction.UserId, Balance = insertedTransaction.Sum },
(insertedTransaction, oldBalance) => new UserBalance { Balance = oldBalance.Balance + insertedTransaction.Sum })));
This code will be translated into sql and applied to migrations using
migrationBuilder.Sql()