WPF【11_2】WPF实战-重构与美化(Entity Framework)-示例
示例:Entity Framework Core应用修改第10章(客户预约表例子)
--\Models\Customer.cs
public partial class Customer
{
public Customer()
{
Appointments = new HashSet<Appointment>();
}
public int Id { get; set; }
public string Name { get; set; }
public string IdNnumber { get; set; }
public string Address { get; set; }
public virtual ICollection<Appointment> Appointments { get; set; }
}
--\Models\Appointment.cs
public partial class Appointment
{
public int Id { get; set; }
public DateTime Time { get; set; }
public int? CustomerId { get; set; }
public virtual Customer Customer { get; set; }
}
--\Models\AppDbContext.cs
public partial class AppDbContext : DbContext
{
public AppDbContext()
{
}
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public virtual DbSet<Appointment> Appointments { get; set; }
public virtual DbSet<Customer> Customers { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer("Data Source=localhost;Initial Catalog=course565;Persist Security Info=True;User ID=sa;Password=PaSSword12!;Pooling=False");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasAnnotation("Relational:Collation", "SQL_Latin1_General_CP1_CI_AS");
modelBuilder.Entity<Appointment>(entity =>
{
entity.Property(e => e.Time).HasColumnType("datetime");
entity.HasOne(d => d.Customer)
.WithMany(p => p.Appointments)
.HasForeignKey(d => d.CustomerId)
.HasConstraintName("FK_Appointments_Customers");
});
modelBuilder.Entity<Customer>(entity =>
{
entity.Property(e => e.Address)
.IsRequired()
.HasMaxLength(100)
.IsFixedLength(true);
entity.Property(e => e.IdNnumber)
.IsRequired()
.HasMaxLength(50)
.IsFixedLength(true);
entity.Property(e => e.Name)
.IsRequired()
.HasMaxLength(50)
.IsFixedLength(true);
});
OnModelCreatingPartial(modelBuilder);
}
partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
--\MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ShowCustomers();
}
private void ShowCustomers()
{
try
{
using (var db = new AppDbContext())
{
var customers = db.Customers.ToList();
customerList.DisplayMemberPath = "Name";
customerList.SelectedValuePath = "Id";
customerList.ItemsSource = customers;
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
private void customerList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
Customer selectedItem = customerList.SelectedItem as Customer;
if (selectedItem == null)
{
appointmentList.ItemsSource = null;
return;
}
NameTextBox.Text = selectedItem.Name;
IdTextBox.Text = selectedItem.IdNnumber;
AddressTextBox.Text = selectedItem.Address;
using (var db = new AppDbContext())
{
var customerId = customerList.SelectedValue;
var appointment = db.Appointments.Where(a => a.CustomerId == (int)customerId).ToList();
appointmentList.DisplayMemberPath = "Time";
appointmentList.SelectedValuePath = "Id";
appointmentList.ItemsSource = appointment;
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
}
private void DeleteAppointment_Click(object sender, RoutedEventArgs e)
{
try
{
var appointmentId = appointmentList.SelectedValue;
using (var db=new AppDbContext())
{
var appointmentToRmove = db.Appointments.Where(a => a.Id == (int)appointmentId).FirstOrDefault();
db.Appointments.Remove(appointmentToRmove);
db.SaveChanges();
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
customerList_SelectionChanged(null, null);
}
}
private void DeleteCustomer_Click(object sender, RoutedEventArgs e)
{
try
{
var customerId = customerList.SelectedValue;
using (var db = new AppDbContext())
{
var customerToRemove = db.Customers
.Include(c => c.Appointments)
.Where(c => c.Id == (int)customerId)
.FirstOrDefault();
db.Customers.Remove(customerToRemove);
db.SaveChanges();
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
ShowCustomers();
customerList_SelectionChanged(null, null);
}
}
private void AddCustomer_Click(object sender, RoutedEventArgs e)
{
try
{
using (var db = new AppDbContext())
{
var customer = new Customer()
{
Name = NameTextBox.Text,
IdNnumber = IdTextBox.Text,
Address = AddressTextBox.Text
};
db.Customers.Add(customer);
db.SaveChanges();
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
ShowCustomers();
}
}
private void AddAppointment_Click(object sender, RoutedEventArgs e)
{
try
{
using (var db = new AppDbContext())
{
var appointment = new Appointment()
{
Time = DateTime.Parse(AppointmentDatePicker.Text),
CustomerId = (int)customerList.SelectedValue
};
db.Appointments.Add(appointment);
db.SaveChanges();
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
customerList_SelectionChanged(null, null);
}
}
private void UpdateCustomer_Click(object sender, RoutedEventArgs e)
{
try
{
using (var db=new AppDbContext())
{
var customer = db.Customers.Where(c => c.Id == (int)customerList.SelectedValue).FirstOrDefault();
customer.Name = NameTextBox.Text.Trim();
customer.IdNnumber = IdTextBox.Text.Trim();
customer.Address = AddressTextBox.Text.Trim();
db.SaveChanges();
}
}
catch (Exception error)
{
MessageBox.Show(error.ToString());
}
finally
{
ShowCustomers();
}
}
}