Hi @Dotnet Engineer
Agree with Johan Smarius, it is better to create a trigger to insert the new records into the appointmentaudit table.
You can try to directly create the insert trigger on the database side, using the following query statement:
create Trigger function:
CREATE OR REPLACE FUNCTION function_copy() RETURNS TRIGGER AS
$BODY$
BEGIN
INSERT INTO
public.appointmentaudit(appid,notes,appdate,patientid,clinicianid,statusid)
VALUES(new.appid,new.notes,new.appdate,new.patientid,new.clinicianid,new.statusid);
RETURN new;
END;
$BODY$
language plpgsql;
Create Trigger:
CREATE TRIGGER trig_copy
AFTER INSERT ON public.appointment
FOR EACH ROW
EXECUTE PROCEDURE function_copy();
Since we are using AFTER INSERT, after insert the new item into the public.appointment table, it will trigger this function_copy and copy the new item to the public.appointmentaudit table.
Or, if you want to create the trigger via EF core, after executing the add-migration
migration command, you can execute the create trigger sql command in the migration file, like this:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"create trigger .....");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"drop trigger <triggerName>");
}
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion