Custom map between model property and table column using ModelToTableMapper<T>

When model has properties with different name from table's column, we can use ModelToTableMapper<T> for mapping:

Assuming a database table as:

CREATE TABLE [Customers] (
	[Id] [int] IDENTITY(1, 1) NOT NULL,
	[First Name] [nvarchar](50) NULL,
	[Last Name] [nvarchar](50) NULL)

Assuming a C# model as:

    public class Customer
        public int Id { get; set; }
        public string Name { get; set; }
        public string Surname { get; set; }

In order to receive notifications containing record's values changed, inserted or deleted, the following code can be used:

var mapper = new ModelToTableMapper<Customer>();
mapper.AddMapping(c => c.Name, "First Name");
mapper.AddMapping(c => c.Surname, "Last Name");

var tableName = "Customers";
var tableDependency = new OracleTableDependency<Customer>(connectionString, tableName, mapper);
tableDependency.OnChanged += TableDependency_Changed;

This setting override Code First Data Annotations.

Last edited Nov 14, 2015 at 4:53 PM by delbianco, version 3