using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;


namespace WPDatabaseEntitySet
{
    [Table(Name="Persons")]
    public class Person : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int id;
        private string name;
        private string firstName;
        private int age;
        private string city;
        private EntitySet<Order> orders;

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ID
        {
            get
            {
                return id;
            }
            private set
            {
                if (id != value)
                {
                    NotifyPropertyChanging("ID");
                    id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        [Column]
        public string LastName
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    NotifyPropertyChanging("LastName");
                    name = value;
                    NotifyPropertyChanged("LastName");
                }
            }
        }

        [Column]
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                if (firstName != value)
                {
                    NotifyPropertyChanging("FirstName");
                    firstName = value;
                    NotifyPropertyChanged("FirstName");
                }
            }
        }

        [Column]
        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (age != value)
                {
                    NotifyPropertyChanging("Age");
                    age = value;
                    NotifyPropertyChanged("Age");
                }
            }
        }

        [Column]
        public string City
        {
            get
            {
                return city;
            }
            set
            {
                if (city != value)
                {
                    NotifyPropertyChanging("City");
                    city = value;
                    NotifyPropertyChanged("City");
                }
            }
        }

        // Benötigt für höhere Geschwindigkeit
        [Column(IsVersion = true)]
        private Binary version;

        [Association(Storage = "orders", ThisKey="ID", OtherKey = "PersonID")]
        public EntitySet<Order> Orders
        {
            get
            {
                if (orders==null)
                {
                    orders = new EntitySet<Order>();
                }
                return orders;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }

    [Table(Name="Order")]
    public class Order : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private int id;
        private string name;
        public int? personID;

        private EntityRef<Person> person;

        [Association(Storage = "person", IsForeignKey = true, ThisKey="PersonID", OtherKey = "ID")]
        public Person Person
        {
            get
            {
                return person.Entity;
            }
            set
            {
                if (value != person.Entity && value != null)
                {
                    NotifyPropertyChanging("Person");
                    person.Entity = value;
                    personID = value.ID;
                    NotifyPropertyChanged("Person");
                }
            }
        }

        [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
        public int ID
        {
            get
            {
                return id;
            }
            private set
            {
                if (id != value)
                {
                    NotifyPropertyChanging("ID");
                    id = value;
                    NotifyPropertyChanged("ID");
                }
            }
        }

        [Column]
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (name != value)
                {
                    NotifyPropertyChanging("LastName");
                    name = value;
                    NotifyPropertyChanged("LastName");
                }
            }
        }

        [Column]
        public int? PersonID
        {
            get
            {
                return personID;
            }
            set
            {
                if (personID != value)
                {
                    NotifyPropertyChanging("PersonID");
                    personID = value;
                    NotifyPropertyChanged("PersonID");
                }
            }
        }

        // Benötigt für höhere Geschwindigkeit

        [Column(IsVersion = true)]
        private Binary version;

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        private void NotifyPropertyChanging(string propertyName)
        {
            if (PropertyChanging != null)
            {
                PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }
    }
}
