12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- using WpfApp1.Models;
- namespace WpfApp1
- {
- /// <summary>
- /// Логика взаимодействия для AddOrUpdateClient.xaml
- /// </summary>
- public partial class AddOrUpdateClient : Window
- {
- ApplicationContext db = new ApplicationContext();
- Clients client;
- public AddOrUpdateClient()
- {
- InitializeComponent();
- LoadDatabase();
- client = new Clients();
- }
- public AddOrUpdateClient(int id)
- {
- InitializeComponent();
- LoadDatabase();
- client = db.Clients.Local.FirstOrDefault(x => x.ID == id);
- tbSurname.Text = client.Surname;
- tbName.Text = client.Name;
- tbPhoneNumber.Text = client.PhoneNumber;
- tbE_mail.Text = client.E_mail;
- }
- void LoadDatabase()
- {
- // гарантируем, что база данных создана
- db.Database.EnsureCreated();
- // загружаем данные из БД
- db.Clients.Load();
- }
- private void btnSave_Click(object sender, RoutedEventArgs e)
- {
- try
- {
- client.Surname = tbSurname.Text;
- client.Name = tbName.Text;
- client.PhoneNumber = tbPhoneNumber.Text;
- client.E_mail = tbE_mail.Text;
- if (client.ID == 0)
- {
- db.Clients.Add(client);
- }
- db.SaveChanges();
- MessageBox.Show("Клиент добавлен");
- Close();
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- }
|