win7user10 / Laraue.EfCoreTriggers

Library to write triggers in C# with EF.Core
MIT License
111 stars 20 forks source link

add a trigger for updating a specific column #95

Open Vasypu opened 8 months ago

Vasypu commented 8 months ago

Is it possible to add to the library support for triggers that are triggered only when certain columns in the table are updated?

expected sql of such a trigger:

CREATE TRIGGER trg_update_trigger
AFTER UPDATE OF column1 ON your_table
FOR EACH ROW
EXECUTE FUNCTION your_update_function();

syntax how it would look like:

modelBuilder.Entity<YourEntity>()
            .AfterUpdateOf(x => x.column1
                .Action(x => x.ExecuteRawSql("some sql")));
win7user10 commented 8 months ago

You can try something like

modelBuilder.Entity<YourEntity>()
            .AfterUpdate(trigger => trigger
                .Condition(refs => refs.Old.Column1 != refs.New.Column1)
                .ExecuteRawSql("some sql"));
Vasypu commented 8 months ago

that's what I did, but this code generates the following sql

CREATE FUNCTION myFunction() RETURNS trigger as $LC_TRIGGER_AFTER_UPDATE_MYFUNCTION$
BEGIN
    IF NEW."Column1" <> OLD."Column1" OR NEW."Column2" <> OLD."Column2" THEN
    ....
    END IF;
RETURN NEW;
END;
$LC_TRIGGER_AFTER_UPDATE_MYFUNCTION$ LANGUAGE plpgsql;

CREATE TRIGGER LC_TRIGGER_AFTER_UPDATE_MYFUNCTION AFTER UPDATE
ON your_table
FOR EACH ROW EXECUTE PROCEDURE myFunction();

and I need the trigger condition to be triggered not in the trigger body, as it is here.

CREATE TRIGGER trg_update_trigger
AFTER UPDATE OF column1 ON your_table
FOR EACH ROW
EXECUTE FUNCTION your_update_function();

is there any possibility with the next update to add support for triggers on certain table columns?