using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

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

namespace WPDatabaseDeleteCascade
{
    public class PersonenDataContext : DataContext, INotifyPropertyChanged
    {
        public static string DBConnectionString = "Data Source=isostore:/PersonenOrder.sdf";

        public PersonenDataContext(string connectionString)
            : base(connectionString)
        {
            if (!DatabaseExists())
            {
                CreateDatabase();
            }
            else
            {
                DeleteDatabase();
                CreateDatabase();
            }
            var personenInDB = from Person person in PersonenTable
                               select person;
            Personen = new ObservableCollection<Person>(personenInDB);
        }

        public Table<Person> PersonenTable;
        public Table<Order> OrderTable;

        private ObservableCollection<Person> personen;
        public ObservableCollection<Person> Personen
        {
            get
            {
                return personen;
            }
            set
            {
                if (personen != value)
                {
                    personen = value;
                    NotifyPropertyChanged("Personen");
                }
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

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