Beispiel #1
0
        static void CRUDExampleOnParts(string connectionString)
        {
            using (var context = new AutoRepairContext(connectionString))
            {
                context.Parts.Add(new Part()
                {
                    Name = "Gear", Price = 100
                });                                                           //Create
                context.SaveChanges();
                Console.WriteLine("Запись создана");
                Console.ReadLine();

                var part = context.Parts.First(x => x.Name == "Gear"); //Read
                Console.WriteLine($"Запись получена: {part.Id} {part.Name} {part.Price}");
                Console.ReadLine();

                part.Price = 150;
                context.Update(part); //Update
                context.SaveChanges();
                Console.WriteLine("Запись обновлена");
                Console.ReadLine();

                context.Remove(part); //Delete
                context.SaveChanges();
                Console.WriteLine("Запись удалена");
                Console.ReadLine();
            }
        }
        public ActionResult <Motorcycle> Post([FromBody] Motorcycle moto)
        {
            //chjeck to make sure this motorcycle dosen't already exist in the database


            var isExistingMotorcycle = _context.Motorcycles.Any(m => m.Id == moto.Id);

            if (isExistingMotorcycle)
            {
                return(BadRequest("Duplicate. Motorcycle already exists in database"));
            }

            //add to database
            _context.Motorcycles.Add(moto);
            _context.Entry(moto).State = Microsoft.EntityFrameworkCore.EntityState.Added;
            _context.SaveChanges();

            return(Ok(moto));
        }
Beispiel #3
0
        public ActionResult <Automobile> Post([FromBody] Automobile auto)
        {
            //check to make sure this automobile doesn't already exist in the database


            var isExistingAutomobile = _context.Automobiles.Any(a => a.Id == auto.Id);

            if (isExistingAutomobile)
            {
                return(BadRequest("Duplicate. Automobile already exists in database."));
            }

            //add to database
            _context.Automobiles.Add(auto);
            _context.Entry(auto).State = Microsoft.EntityFrameworkCore.EntityState.Added;
            _context.SaveChanges();

            return(Ok(auto));
        }
Beispiel #4
0
 public void SaveOrder(Order order)
 {
     _context.SaveChanges();
 }
Beispiel #5
0
        /// <summary>
        /// Пересоздает базу (удаляет и создает заново), заполняет её тестовыми данными
        /// </summary>
        public static void ResetAndFillDatabase(string connectionString)
        {
            using (var context = new AutoRepairContext(connectionString, resetDatabase: true))
            {
                //Заполняем заказчиков
                context.Customers.Add(new Customer()
                {
                    Name = "Hedy Greene", Address = "Ap #696-3279 Viverra. Avenue Latrobe DE 38100"
                });
                context.Customers.Add(new Customer()
                {
                    Name = "Joan Romero", Address = "Lacinia Avenue Idaho Falls Ohio"
                });
                context.Customers.Add(new Customer()
                {
                    Name = "Davis Patrick", Address = "2546 Sociosqu Rd. Bethlehem Utah"
                });

                //Заполняем машины
                context.Vehicles.Add(new Vehicle()
                {
                    Make = "Ford", Model = "F-Series", RegistrationPlate = "798 PAK", Year = 2007
                });
                context.Vehicles.Add(new Vehicle()
                {
                    Make = "Chevrolet", Model = "Silverado", RegistrationPlate = "GHT430", Year = 2003
                });
                context.Vehicles.Add(new Vehicle()
                {
                    Make = "Suzuki", Model = "Aerio", RegistrationPlate = "50D24H8", Year = 2011
                });

                //Заполняем список запчастей
                context.Parts.Add(new Part()
                {
                    Name = "Front Clip", Price = 5
                });
                context.Parts.Add(new Part()
                {
                    Name = "Airbag sensor", Price = 10
                });
                context.Parts.Add(new Part()
                {
                    Name = "Starter Pinion Gear", Price = 50
                });
                context.Parts.Add(new Part()
                {
                    Name = "Differential", Price = 1500
                });
                context.Parts.Add(new Part()
                {
                    Name = "Backup Camera", Price = 115
                });
                context.Parts.Add(new Part()
                {
                    Name = "Door Contact", Price = 75
                });

                //Заполняем механиков
                context.Workers.Add(new Worker()
                {
                    Name = "Leilani Boyer", Position = "Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Edward Nieves", Position = "Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Christian Emerson", Position = "St. Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Raymond Levy", Position = "Jr. Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Tom Adams", Position = "Jr. Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "John Patel", Position = "Jr. Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Pat Quinn", Position = "Jr. Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Mike Usman", Position = "Jr. Mechanic"
                });
                context.Workers.Add(new Worker()
                {
                    Name = "Diego Yakub", Position = "Jr. Mechanic"
                });

                context.SaveChanges();
            }
        }