Exemple #1
0
        private void trunDbBtn_Click(object sender, EventArgs e)
        {
            try
            {
                using (var context = new ApplicationDbContext())
                {
                    var listener = new ProgressBarListener()
                    {
                        Context = context, Bar = sqlProgressBar, StatusLbl = sqlStatusLbl
                    };
                    listener.listen();

                    List <string> tableList = context.Model.GetEntityTypes()
                                              .Select(t => t.GetTableName())
                                              .Distinct()
                                              .ToList();

                    foreach (string t in tableList)
                    {
                        string prefix = "dbo.";
                        string name   = prefix + t;

                        context.Database.ExecuteSqlRaw("DELETE FROM " + name);
                    }

                    context.SaveChanges();
                }
            } catch (DbUpdateException ex)
            {
                debugBox.Text = "An error occured while saving changes\n" + ex.Message;
            }
        }
Exemple #2
0
        private void fillDbBtn_Click(object sender, EventArgs e)
        {
            try
            {
                using (var context = new ApplicationDbContext())
                {
                    var listener = new ProgressBarListener {
                        Context = context, Bar = sqlProgressBar, StatusLbl = sqlStatusLbl
                    };
                    listener.listen();

                    var exampleK1 = new Kunde
                    {
                        Anrede      = 0,
                        Vorname     = "Max",
                        Nachname    = "Mustermann",
                        Adresse     = "Musterstr. 1",
                        Plz         = 12345,
                        Ort         = "Musterstadt",
                        Telefon     = "+49123 456789",
                        Email       = "*****@*****.**",
                        Bemerkungen = "",
                        Rechnungen  = new List <Rechnung>
                        {
                            new Rechnung {
                                KundeId = 1, ObjektId = 1, Datum = new DateTime(2021, 05, 19, 10, 30, 00), Taetigkeiten = "(automatisch generiert)", Material = "(automatisch generiert)", Arbeitszeit = new TimeSpan(1, 30, 0), Summe = 65, Bemerkungen = "(automatisch generiert)"
                            },
                            new Rechnung {
                                KundeId = 1, ObjektId = 1, Datum = new DateTime(2021, 05, 26, 14, 00, 00), Taetigkeiten = "(automatisch generiert)", Material = "(automatisch generiert)", Arbeitszeit = new TimeSpan(1, 30, 0), Summe = 65, Bemerkungen = "(automatisch generiert)"
                            },
                        },
                        Objekte = new List <Objekt>
                        {
                            new Objekt {
                                KundeId = 1, Adresse = "Musterstr. 2", Plz = 12345, Ort = "Musterstadt", Bemerkungen = ""
                            },
                        }
                    };

                    context.Kunden.Add(exampleK1);
                    context.SaveChanges();
                }
            } catch (DbUpdateException ex)
            {
                debugBox.Text = "An error occured while saving changes\n" + ex.Message;
            }
        }