I have the following code to create SQL setup a trigger (not yet a prepared statement).
commandList.Add($"DROP TRIGGER if exists `{tableName}_after_insert`;");
commandList.Add($"delimiter $$" + Environment.NewLine +
$"CREATE TRIGGER `{tableName}_after_insert` AFTER INSERT" + Environment.NewLine +
$"ON `{tableName}`" + Environment.NewLine +
"FOR EACH ROW BEGIN" + Environment.NewLine +
"Insert into HashEntry(TableName, TableRowId, TokenValue)" + Environment.NewLine +
$"select '{tableName}', n.{tableKey}, 'tokenValue' from NEW n;" + Environment.NewLine +
"END;" + Environment.NewLine +
"$$" + Environment.NewLine +
"delimiter ;");
//then some unrelated code until we execute
foreach (var command in commandList)
{
Database.ExecuteSqlCommand(command);
}
This create the following SQL which works in Workbench and Entity Framework.
DROP TRIGGER if exists `MyTable_after_insert`;
And this which only works in Workbench directly.
delimiter $$
CREATE TRIGGER `MyTable_after_insert` AFTER INSERT
ON `MyTable`
FOR EACH ROW BEGIN
Insert into HashEntry(TableName, TableRowId, TokenValue)
select 'MyTable', n.Id, 'tokenValue' from NEW n;
END;
$$
delimiter ;
Which gives the following error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delimiter $$
CREATE TRIGGER `MyTable_after_insert` AFTER INSERT
I'm not sure why it would work directly in Workbench but not calling the same command through EF.
Any ideas?