private void delete_btn(object sender, RoutedEventArgs e)
        {
            string data = this.DeleteListGrid.SelectedItem.ToString().Trim('}', '{').Trim();
            var    q    = data.Split(',')
                          .Select(x => x.Split('='))
                          .Select(x => x[1].Trim());
            string u = "";

            foreach (var item in q)
            {
                u += (" " + item);
            }
            string[] q2 = u.Trim().Split(' ');
            string   Im = q2[0];
            string   Na = q2[1];

            using (OwnerEntities db = new OwnerEntities())
            {
                var email = from p in db.EmailAddresses
                            where p.PersonID == db.People.Where(x => x.fName == Im).Where(x => x.lName == Na).FirstOrDefault().ID
                            select p;

                int temp = email.Count();
                for (int i = 0; i < temp; i++)
                {
                    db.EmailAddresses.Remove(email.FirstOrDefault());
                }
                db.SaveChanges();
                var numbers = from p in db.PhoneNumbers
                              where p.PersonID == db.People.Where(x => x.fName == Im).Where(x => x.lName == Na).FirstOrDefault().ID
                              select p;

                temp = numbers.Count();

                for (int i = 0; i < temp; i++)
                {
                    db.PhoneNumbers.Remove(numbers.FirstOrDefault());
                }
                db.SaveChanges();
                var people = from p in db.People
                             where p.fName == Im
                             where p.lName == Na
                             select p;

                db.People.Remove(people.Include(x => x.Countries).Include(x => x.EmailAddresses).Include(x => x.PhoneNumbers).First());
                db.SaveChanges();
            }
        }
Example #2
0
        public MainWindow(string x, string y)
        {
            InitializeComponent();
            this.Current_Owner.Text = x + " " + y;
            ActualOwner.fName       = x;
            ActualOwner.lName       = y;


            DataContext = new Start_model();


            using (OwnerEntities db = new OwnerEntities())
            {
                ActualOwner.ID = db.Owners.Where(o => o.fName == x).Where(o => o.lName == y).FirstOrDefault().ID;


                if (db.Countries.Count() == 0)
                {
                    foreach (var item in Country.GetValues(typeof(Country)))
                    {
                        Countries c = new Countries()
                        {
                            Country = item.ToString()
                        };
                        db.Countries.Add(c);
                    }
                    db.SaveChanges();
                } // wypełnienie bazy Krajami z enum

                if (db.PhoneBooks.Where(a => a.OwnerID == db.Owners.Where(o => o.fName == ActualOwner.fName).Where(o => o.lName == ActualOwner.lName).FirstOrDefault().ID).Count() == 0)
                {
                    PhoneBooks book = new PhoneBooks()
                    {
                        OwnerID = db.Owners.Where(o => o.fName == ActualOwner.fName).Where(o => o.lName == ActualOwner.lName).First().ID
                    };
                    db.PhoneBooks.Add(book);
                    db.SaveChanges();
                }  //stworzenie książki dla aktualnego usera, jeżeli nie istnieje
            }
        }
Example #3
0
        /// <summary>
        /// Dodanie danych do bazy
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Add_btn(object sender, RoutedEventArgs e)
        {
            using (OwnerEntities db = new OwnerEntities())
            {
                try
                {
                    var eMailValidator = new System.Net.Mail.MailAddress(Mail.Text); //Sprawdzenie Czy adres E-mail jest prawidłowy

                    int    countryID = db.Countries.Where(x => x.Country.Contains(Country_box.Text)).First().ID;
                    string a         = Full_name.Text.Split(' ')[1].ToString();
                    string b         = Full_name.Text.Split(' ')[0].ToString();

                    var q = db.People.Where(x => x.fName == b).Where(x => x.lName == a).Where(x => x.PhoneBookID == db.PhoneBooks.Where(o => o.OwnerID == ActualOwner.ID).FirstOrDefault().ID);
                    if (q.Count() == 0)
                    {
                        People person = new People()
                        {
                            fName       = this.Full_name.Text.Split(' ')[0],
                            lName       = this.Full_name.Text.Split(' ')[1],
                            CountryID   = countryID,
                            PhoneBookID = db.PhoneBooks.Where(x => x.OwnerID == db.Owners.Where(o => o.fName == ActualOwner.fName).Where(o => o.lName == ActualOwner.lName).FirstOrDefault().ID).First().ID
                        };
                        db.People.Add(person);
                        db.SaveChanges();
                    }
                    else
                    {
                        MessageBox.Show("Ta osoba istnieje. Dodano dodatkowy adres email i numer telefonu.");
                    }

                    EmailAddresses email = new EmailAddresses()
                    {
                        Email    = this.Mail.Text.ToString(),
                        PersonID = db.People.Where(x => x.fName == b).Where(x => x.lName == a).Where(x => x.PhoneBookID == db.PhoneBooks.Where(o => o.OwnerID == ActualOwner.ID).FirstOrDefault().ID).FirstOrDefault().ID
                    };

                    PhoneNumbers number = new PhoneNumbers()
                    {
                        Number   = this.Phone.Text.ToString(),
                        PersonID = db.People.Where(x => x.fName == b).Where(x => x.lName == a).Where(x => x.PhoneBookID == db.PhoneBooks.Where(o => o.OwnerID == ActualOwner.ID).FirstOrDefault().ID).FirstOrDefault().ID
                    };



                    //db.PhoneBooks.Add(book);
                    db.EmailAddresses.Add(email);
                    db.PhoneNumbers.Add(number);
                    db.SaveChanges();

                    this.Full_name.Text = "";
                    this.Phone.Text     = "";
                    this.Mail.Text      = "";
                }
                catch (Exception)
                {
                    MessageBox.Show("Podany Email jest nieprawidłowy. Spróbuj ponownie");
                    Mail.Text  = "";
                    Phone.Text = "";
                }
            }
        }
        /// <summary>
        /// Przycisk odpowiedzialny za usuwanie obiektów z bazy.
        /// </summary>
        /// <param name="sender">Nie mam pojęcia co to jest</param>
        /// <param name="e"></param>
        private void delete_Click(object sender, RoutedEventArgs e)
        {
            OwnerEntities db = new OwnerEntities();


            //dodaj using (

            if (db.Owners.Count() != 0)
            {
                var q = db.Owners;
                foreach (var item in q)
                {
                    db.Owners.Remove(db.Owners.Include(x => x.PhoneBooks).First());
                }
            }
            if (db.People.Count() != 0)
            {
                var q = db.People;
                foreach (var item in q)
                {
                    db.People.Remove(db.People.OrderBy(x => x.ID).Include(x => x.Countries).Include(x => x.EmailAddresses).Include(x => x.PhoneNumbers).First());
                }
            }
            if (db.PhoneBooks.Count() != 0)
            {
                var q = db.PhoneBooks;
                foreach (var item in q)
                {
                    db.PhoneBooks.Remove(db.PhoneBooks.Include(x => x.Owners).Include(x => x.People).First());
                }
            }
            if (db.EmailAddresses.Count() != 0)
            {
                var q = db.EmailAddresses;
                foreach (var item in q)
                {
                    db.EmailAddresses.Remove(db.EmailAddresses.First());
                }
            }
            if (db.PhoneNumbers.Count() != 0)
            {
                var q = db.PhoneNumbers;
                foreach (var item in q)
                {
                    db.PhoneNumbers.Remove(db.PhoneNumbers.First());
                }
            }
            //if (db.Countries.Count() != 0)
            //{
            //    var q = db.Countries;
            //    foreach (var item in q)
            //    {
            //        db.Countries.Remove(db.Countries.First());
            //    }
            //}

            //db.Database.ExecuteSqlCommand("DELETE FROM [Owners]");
            db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Owner.dbo.Owners', RESEED, 0)");
            db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Owner.dbo.EmailAddresses', RESEED, 0)");
            db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Owner.dbo.People', RESEED, 0)");
            db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Owner.dbo.PhoneBooks', RESEED, 0)");
            db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Owner.dbo.PhoneNumbers', RESEED, 0)");
            //db.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Owner.dbo.Countries', RESEED, 0)");
            db.SaveChanges();
        }