예제 #1
0
        // добавление
        private void Add_Click(object sender, RoutedEventArgs e)
        {
            CarWindow carWindow = new CarWindow(new Car(), db);

            if (carWindow.ShowDialog() == true)
            {
                Car car = carWindow.Car;
                db.Cars.Add(car);
                db.SaveChanges();
            }
        }
예제 #2
0
        // редактирование
        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            // если ни одного объекта не выделено, выходим
            if (carList.SelectedItem == null)
            {
                return;
            }
            // получаем выделенный объект
            Car Car = carList.SelectedItem as Car;

            CarWindow carWindow = new CarWindow(new Car
            {
                Id        = Car.Id,
                CarNumber = Car.CarNumber,
                Mark      = Car.Mark,
                Model     = Car.Model,
                ClientId  = Car.ClientId,
                MasterId  = Car.MasterId,
                WorkId    = Car.WorkId
            }, db);

            if (carWindow.ShowDialog() == true)
            {
                // получаем измененный объект
                Car = db.Cars.Find(carWindow.Car.Id);
                if (Car != null)
                {
                    Car.CarNumber       = carWindow.Car.CarNumber;
                    Car.Mark            = carWindow.Car.Mark;
                    Car.Model           = carWindow.Car.Model;
                    Car.ClientId        = carWindow.Car.ClientId;
                    Car.MasterId        = carWindow.Car.MasterId;
                    Car.WorkId          = carWindow.Car.WorkId;
                    db.Entry(Car).State = EntityState.Modified;
                    db.SaveChanges();
                }
                carList.Items.Refresh();
            }
        }