-
Notifications
You must be signed in to change notification settings - Fork 177
Use case: Model and properties with same name of table and columns
christiandelbianco edited this page Jun 21, 2017
·
1 revision
Assuming a database table as:
CREATE TABLE [Customers](
[Id] [int] IDENTITY(1, 1) NOT NULL,
[Name] [nvarchar](50) NULL,
[Surname] [nvarchar](50) NULL)
Assuming a C# model as:
public class Customers
{
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public DateTime Born { get; set; }
public int Quantity { get; set; }
}
In order to receive notifications containing record's values changed, inserted or deleted, the following code can be used:
string conString = "data source=.;initial catalog=myDB;integrated security=True";
using(var tableDependency = new SqlTableDependency<Customers>(conString))
{
tableDependency.OnChanged += TableDependency_Changed;
tableDependency.Start();
Console.WriteLine("Waiting for receiving notifications...");
Console.WriteLine("Press a key to stop");
Console.ReadKey();
}
...
...
void TableDependency_Changed(object sender, RecordChangedEventArgs<Customers> e)
{
if (e.ChangeType != ChangeType.None)
{
var changedEntity = e.Entity;
Console.WriteLine("DML operation: " + e.ChangeType);
Console.WriteLine("ID: " + changedEntity.Id);
Console.WriteLine("Name: " + changedEntity.Name);
Console.WriteLine("Surname: " + changedEntity.Surname);
}
}
Mapping between model property and column name is done by name, as well as model name and table name. Note that model properties without match with table column name are ignored.