Watch for a table new records in sql database

.net-4.5 c# entity-framework sql-server


I am using EF in a windows application and I want my application to do some tasks when a new record inserted in a certain table "these new records will be inserted by a website using the same db"
My question is how to watch this table for changes and get notified when a new record come, and can EF help me in this case?

UPDATE: I used the SqlDependency Class and used this in the db


And also created a service and a queue in the database but I never get notified from my windows application.
Also the when i open the queue in sql server it is always empty seams that there is something wrong but i don't know.

3/11/2016 8:15:37 AM

Popular Answer

Here are my propositions:

  1. If you are able to add duplicate table to database then here is a solution. You have your table1 and table2 (Copy of table1). When you inserting new records to table1, you can compare them with existing records in your table2 and thus find new records. After comparing you should add all new records to table2. This is some kind of synchronization. This can be made via stored proc or programatically.

  2. You don't need any another tables. You can store all your data in your app cache and check with some period of time (for example 5secs) are there any new events, that aren't exist in your cache. If they aren't exist - notify them in your log or somewhere else and add them to cache. But if there are too many records, the processing time will be greatly increased + memory consumption.

  3. If you are able to change db then you can add something like 'isNew' column to your table. When a new data came from the website, the column will be 'true', your program can track this, and after processing set this flag to false for each record. (If the website can't set this flag, you can use SQL TRIGGER AFTER INSERT to set the flag value to true. Website can't even know about this feature if it is third-party web site or you don't want to change anything there)

  4. Here is article about EF changes tracking:

But the problem is that you should check whole table for changes via EF that will hit your app performance.

Here are useful info about SQL Server side change tracking and implementation concepts:

3/13/2013 10:31:09 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow