Click here to Skip to main content
15,889,403 members
Articles / Programming Languages / SQL
Tip/Trick

SQL Dependency using Entity Framework in WPF

Rate me:
Please Sign up or sign in to vote.
4.47/5 (5 votes)
6 Apr 2018CPOL 12.5K   9   1
SQL table dependency with entity framework in WPF

Introduction

This project will help you understand how to fetch data in WPF without refreshing the UI content and notify as well.

Using the Code

The first step is to create person class:

SQL
CREATE TABLE Persons
(
    Id int NOT NULL PRIMARY KEY,
    FirstName varchar(255) NOT NULL,
    LastName varchar(255) NOT NULL
); 

Next, enable Service Broker for the database using the following command:

SQL
ALTER DATABASE DatabaseName SET ENABLE_BROKER

Then, prepare a class which represents the table from the database:

C#
public class Person
{
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }
}

After that, create an object instance of SqlTableDependency class passing table name and connection string as parameters. Afterwards, subscribe a method to the change event and invoke Start method to begin receiving table change notifications:

C#
public void WatchTable()
    {
         var connectionString = ConfigurationManager.ConnectionStrings
                                     ["DefaultConnString"].ConnectionString;
         var tableName = "[dbo].[Persons]";
         var tableDependency = new SqlTableDependency<Person>(connectionString, tableName);

         tableDependency.OnChanged += OnNotificationReceived;
         tableDependency.Start();
    }

private void OnNotificationReceived(object sender, 
                  TableDependency.EventArgs.RecordChangedEventArgs<Person> e)
    {
        switch (e.ChangeType)
        {
           case ChangeType.Delete:
                Console.WriteLine("{0} {1} has been deleted", e.Entity.FirstName, e.Entity.LastName);
                break;

           case ChangeType.Update:
                Console.WriteLine("{0} {1} has been updated", e.Entity.FirstName, e.Entity.LastName);
                break;

           case ChangeType.Insert:
                Console.WriteLine("{0} {1} has been inserted", e.Entity.FirstName, e.Entity.LastName);
                break;
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) ForbMedia
Pakistan Pakistan
I have recently started my career as WPF developer.Last year i did my internship as WPF (MVVM). I am passionate about the C#.Net technologies. WPF Mvvm is love at first sight, looking forward to gain skills in IoT. I love sharing and gaining knowledge.

Comments and Discussions

 
QuestionNo credit? Pin
Ozan Yasin Dogan8-Dec-19 11:30
Ozan Yasin Dogan8-Dec-19 11:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.