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

namespace WPDatabaseUpgradeSchema
{
    public class PersonenDataContext : DataContext, INotifyPropertyChanged
    {
        public static string DBConnectionString = "Data Source=isostore:/Personen.sdf";
        public readonly int SCHEMAVERSION = 1;

        public PersonenDataContext(string connectionString)
            : base(connectionString)
        {
        }

        public Table<Person> PersonenTable;

        private ObservableCollection<Person> personen;
        public ObservableCollection<Person> Personen
        {
            get
            {
                if (personen == null && DatabaseExists())
                {
                    var personenInDB = from Person person in PersonenTable
                                       select person;
                    personen = new ObservableCollection<Person>(personenInDB);
                    NotifyPropertyChanged("Personen");
                }
                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
    }
}
