using System.Windows;
using Microsoft.Phone.Controls;

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

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

        private void btnCreateEntry_Click(object sender, RoutedEventArgs e)
        {
            if (personenDataContext.Personen.Count == 0)
            {
                Person person = new Person()
                {
                    FirstName = "Simon",
                    LastName = "Hackfort",
                    Age = 32,
                    City = "Duisburg"
                };
                Order[] orders = new Order[2];
                orders[0] = new Order()
                {
                    Name = "Programmieren für WP7",
                    Person=person
                };
                orders[1] = new Order()
                {
                    Name = "Programmieren für WP7 - Teil 2",
                    Person = person
                };
                person.Orders.AddRange(orders);
                personenDataContext.PersonenTable.InsertOnSubmit(person);
                personenDataContext.SubmitChanges();
                personenDataContext.Personen.Add(person);
            }
        }
    }
}