Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ detect insert

Tags:

c#

sql

linq

I have a Web API where i want to trigger a function when a new row has been inserted to a DB table. I don't want to have to get all the data and look for new data manually.

Is it possible to have a SQL Server trigger that somehow calls a function in my C# Web API?

How can this be done? Is it a good idea?

like image 862
Lord Vermillion Avatar asked Sep 21 '26 14:09

Lord Vermillion


1 Answers

You can override SubmitChanges in your DataContext, something like:

public override void SubmitChanges(System.Data.Linq.ConflictMode failureMode)
{

    var set = this.GetChangeSet();//list of pending changes

    //Call your methods here
    foreach (var insert in set.Inserts)
    {
        //Insert Logic 
    }
    foreach (var update in set.Updates)
    {
        //Update Logic 
    }
    foreach (var item in set.Deletes)
    {
        //Delete Logic
    }


    base.SubmitChanges(failureMode);//allow the DataContext to perform it's default work (including your new log changes)

}
like image 65
Sid Avatar answered Sep 23 '26 04:09

Sid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!