protected override void Seed(MyDbContext context)
        {
            Book b1 = new Book()
            {
                Title = "Titanik"
            };
            Book b2 = new Book()
            {
                Title = "Book"
            };
            Sage s1 = new Sage()
            {
                Name = "Bob", Age = new DateTime(2000, 1, 12)
            };
            Sage s2 = new Sage()
            {
                Name = "Jack", Age = new DateTime(1969, 4, 22)
            };

            s1.Books.Add(b1);
            s2.Books.Add(b2);
            context.Books.Add(b1);
            context.Books.Add(b2);
            context.Sages.Add(s1);
            context.Sages.Add(s2);
            context.SaveChanges();
        }
Esempio n. 2
0
 private void DELETEtoolStripButton_Click(object sender, EventArgs e)
 {
     try
     {
         using (MyDbContext cnt = new MyDbContext())
         {
             if (toolStripComboBox1.Text == "Book")
             {
                 if (dataGridView1.SelectedCells.Count > 0)
                 {
                     int findID = Convert.ToInt32(dataGridView1.SelectedCells[0].Value);
                     var res    = cnt.Books.Where(x => x.Id == findID).FirstOrDefault();
                     cnt.Books.Remove(res);
                     cnt.SaveChanges();
                     myRefresh();
                 }
             }
             if (toolStripComboBox1.Text == "Sage")
             {
                 int findID = Convert.ToInt32(dataGridView1.SelectedCells[0].Value);
                 var res    = cnt.Sages.Where(x => x.Id == findID).FirstOrDefault();
                 cnt.Sages.Remove(res);
                 cnt.SaveChanges();
                 myRefresh();
             }
             if (toolStripComboBox1.Text == "SageBook")
             {
                 int  BookTitle = Convert.ToInt32(dataGridView1.SelectedCells[0].Value);
                 int  SageName  = Convert.ToInt32(dataGridView1.SelectedCells[1].Value);
                 Book bToDelete = cnt.Books.First(x => x.Id == BookTitle);
                 Sage sToDelete = cnt.Sages.First(x => x.Id == SageName);
                 sToDelete.Books.Remove(bToDelete);
                 cnt.SaveChanges();
                 myRefresh();
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Esempio n. 3
0
        private void Form2_Load(object sender, EventArgs e)
        {
            try
            {
                if (toSearch == -1)
                {
                    if (fromComboBox == "Sage")
                    {
                        label1.Text       = "Name: ";
                        label2.Text       = "Age: ";
                        comboBox1.Visible = false;
                        comboBox2.Visible = false;
                    }
                    if (fromComboBox == "Book")
                    {
                        label1.Visible    = false;
                        textBox1.Visible  = false;
                        label2.Text       = "Title: ";
                        comboBox1.Visible = false;
                        comboBox2.Visible = false;
                    }
                    if (fromComboBox == "SageBook")
                    {
                        label1.Text = "Book: ";
                        label2.Text = "Sage: ";

                        textBox1.Visible = false;
                        textBox2.Visible = false;
                        using (MyDbContext cnt = new MyDbContext())
                        {
                            comboBox1.DataSource = cnt.Books.Select(x => x.Id).ToList();
                            comboBox2.DataSource = cnt.Sages.Select(x => x.Id).ToList();
                        }
                    }
                }
                else
                {
                    using (MyDbContext cnt = new MyDbContext())
                    {
                        if (fromComboBox == "Sage")
                        {
                            label1.Text       = "Name: ";
                            label2.Text       = "Age: ";
                            s                 = cnt.Sages.First(x => x.Id == toSearch);
                            textBox1.Text     = s.Name;
                            textBox2.Text     = Convert.ToString(s.Age.ToShortDateString());
                            comboBox1.Visible = false;
                            comboBox2.Visible = false;
                        }
                        if (fromComboBox == "Book")
                        {
                            label1.Visible   = false;
                            textBox1.Visible = false;
                            label2.Text      = "Title: ";
                            b                 = cnt.Books.First(x => x.Id == toSearch);
                            textBox2.Text     = b.Title;
                            comboBox1.Visible = false;
                            comboBox2.Visible = false;
                        }
                        if (fromComboBox == "SageBook")
                        {
                            label1.Text      = "Book: ";
                            label2.Text      = "Sage: ";
                            textBox1.Visible = false;
                            textBox2.Visible = false;

                            comboBox1.DataSource = cnt.Books.Select(x => x.Id).ToList();
                            comboBox2.DataSource = cnt.Sages.Select(x => x.Id).ToList();
                            comboBox1.Text       = toSearch.ToString();
                            comboBox2.Text       = toSearchSageBook.ToString();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Esempio n. 4
0
 private void buttonOkay_Click(object sender, EventArgs e)
 {
     try
     {
         using (MyDbContext cnt = new MyDbContext())
         {
             if (toSearch == -1)
             {
                 if (fromComboBox == "Sage")
                 {
                     cnt.Sages.Add(new Sage()
                     {
                         Name = textBox1.Text, Age = DateTime.Parse(textBox2.Text)
                     });
                     cnt.SaveChanges();
                     this.Close();
                 }
                 if (fromComboBox == "Book")
                 {
                     cnt.Books.Add(new Book()
                     {
                         Title = textBox2.Text
                     });
                     cnt.SaveChanges();
                     this.Close();
                 }
                 if (fromComboBox == "SageBook")
                 {
                     int  bID = Convert.ToInt32(comboBox1.Text);
                     int  sID = Convert.ToInt32(comboBox2.Text);
                     Book b   = cnt.Books.First(x => x.Id == bID);
                     Sage s   = cnt.Sages.First(x => x.Id == sID);
                     s.Books.Add(b);
                     cnt.SaveChanges();
                     this.Close();
                 }
             }
             else
             {
                 if (fromComboBox == "Sage")
                 {
                     s      = cnt.Sages.First(x => x.Id == toSearch);
                     s.Name = textBox1.Text;
                     s.Age  = DateTime.Parse(textBox2.Text);
                     cnt.SaveChanges();
                     this.Close();
                 }
                 if (fromComboBox == "Book")
                 {
                     b       = cnt.Books.First(x => x.Id == toSearch);
                     b.Title = textBox2.Text;
                     cnt.SaveChanges();
                     this.Close();
                 }
                 if (fromComboBox == "SageBook")
                 {
                     int bID = Convert.ToInt32(comboBox1.Text);
                     int sID = Convert.ToInt32(comboBox2.Text);
                     cnt.Sages.Find(toSearchSageBook).Books.Remove(cnt.Books.Find(toSearch));
                     cnt.Sages.Find(sID).Books.Add(cnt.Books.Find(bID));
                     cnt.SaveChanges();
                     this.Close();
                 }
             }
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }