This project is read-only.

Code First Data Annotations to map model with database table

In case our table has a name different from Model:

CREATE TABLE CLIENTS (
    ID NUMBER(10), 
    NAME VARCHAR2(50), 
    "Long Description" VARCHAR2(4000));

We can use Code First Data Annotations to achieve mapping:

    [Table("CLIENTS")]
    public class Customers
    {
        public long Id { get; set; }
        public string Name { get; set; }
        [Column("Long Description")]
        public string Surname { get; set; }
    }

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

    string ConnectionString = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT= 1521)))(CONNECT_DATA=(SERVICE_NAME = XE)));User Id=SYSTEM;password=tiger;"

    using(var tableDependency = new OracleTableDependency<Customers>(ConnectionString))
    {
        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);
        }
    }

Last edited Feb 3 at 7:38 PM by delbianco, version 3