using System.Linq;
using Microsoft.Phone.Controls;

namespace WPDatabaseDeleteCascade
{
    public partial class MainPage : PhoneApplicationPage
    {
        private PersonenDataContext personenDataContext;

        // Constructor
        public MainPage()
        {
            InitializeComponent();
            personenDataContext = new PersonenDataContext(PersonenDataContext.DBConnectionString);

            if (personenDataContext.DatabaseExists() == false)
            {
                personenDataContext.CreateDatabase();
            }

            this.DataContext = personenDataContext;

            Person person = new Person()
            {
                FirstName = "Simon",
                LastName = "Hackfort",
                Age = 32,
                City = "Duisburg"
            };

            personenDataContext.PersonenTable.InsertOnSubmit(person);
            personenDataContext.SubmitChanges();

            person = personenDataContext.PersonenTable.FirstOrDefault();

            Order order = new Order()
            {
                Name = "Programmieren für WP7"
            };

            person.Order = order;
            personenDataContext.SubmitChanges();

            var personFromDB = (from Person p in personenDataContext.PersonenTable
                                select p).Single();

            personenDataContext.PersonenTable.DeleteOnSubmit(personFromDB);
            personenDataContext.SubmitChanges();


            var personFromDBCount = (from Person p in personenDataContext.PersonenTable
                                     select p).Count();
            var orderFromDBCount = (from Order o in personenDataContext.OrderTable
                                    select o).Count();
        }
    }
}