DbContext instance represents a database session and is
used to query and save entity instances
OnConfiguring(), which configures the database.
DbContextOptionsBuilder carries configuration information for DbContext and provides
methods to select a database like UseSqlServer() or UseSqlite()
MovieTheaterContext class creates a DbSet of Movie objects to store a list of movies; uses Sqlite database
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkDemo.Models
{
public class MovieTheaterContext : DbContext
{
// Binds to Movies table
public DbSet<Movie> Movies { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Data Source=theater.db");
}
}
}
Movie class to Models folder
INotifyPropertyChanged to support UI data binding
using System.ComponentModel;
namespace EntityFrameworkDemo.Models
{
// All public properties of Movie are converted into table columns
public class Movie : INotifyPropertyChanged
{
// Public property that uses "Id" is the primary key
public int Id { get; set; }
private string title;
public string Title
{
get
{
return title;
}
set
{
title = value;
OnPropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
private string rating;
public string Rating
{
get
{
return rating;
}
set
{
rating = value;
OnPropertyChanged(this, new PropertyChangedEventArgs("Rating"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
PropertyChanged?.Invoke(sender, e);
}
}
}
DbContext to ensure correct use of IDisposable objects
// App.xaml.cs
using EntityFrameworkDemo.Models;
...
namespace MovieTheater.UWP
{
sealed partial class App : Application
{
public App()
{
this.InitializeComponent();
this.Suspending += OnSuspending;
using (var db = new MovieTheaterContext())
{
// Create database
db.Database.EnsureCreated();
}
}
...
}
}
<Page
x:Class="EntityFrameworkDemo.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:EntityFrameworkDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<TextBox Name="Title" PlaceholderText="Title" Width="200" HorizontalAlignment="Left"/>
<ComboBox Name="ratingComboBox" Width="100" HorizontalAlignment="Left" SelectedIndex="1">
<x:String>G</x:String>
<x:String>PG</x:String>
<x:String>PG-13</x:String>
<x:String>R</x:String>
</ComboBox>
<Button Click="addButton_Click" Width="100" HorizontalAlignment="Left">Add</Button>
<ListView Name="movieListView" Width="200" HorizontalAlignment="Left">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Title}" FontWeight="Bold" />
<TextBlock Text="{Binding Rating}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Button x:Name="deleteButton" Content="Delete Movie" Click="deleteButton_Click" />
<Button x:Name="updateButton" Content="Update Movie" Click="updateButton_Click" />
</StackPanel>
</Page>
OnNavigatedTo()
using EntityFrameworkDemo.Models;
using System.Linq;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace EntityFrameworkDemo
{
public sealed partial class MainPage : Page
{
private ObservableCollection<Movie> movieList;
public MainPage()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
// Add all movies from database to movieList
using (var db = new MovieTheaterContext())
{
movieList = new ObservableCollection<Movie>();
foreach (var movie in db.Movies)
{
movieList.Add(movie);
}
}
// Use data binding to display movies in ListView
movieListView.ItemsSource = movieList;
}
}
}
private void addButton_Click(object sender, RoutedEventArgs e)
{
using (var db = new MovieTheaterContext())
{
// Add new movie to database
var movie = new Movie
{
Title = Title.Text,
Rating = ratingComboBox.SelectedValue as string
};
db.Movies.Add(movie);
db.SaveChanges();
// Add to ListView
movieList.Add(movie);
}
}
private void updateButton_Click(object sender, RoutedEventArgs e)
{
// Change selected movie to Tron - PG
if (movieListView.SelectedIndex > -1)
{
using (var db = new MovieTheaterContext())
{
// ListView automatically updated when properties are changed
var selectedMovie = movieListView.SelectedItem as Movie;
selectedMovie.Title = "Tron";
selectedMovie.Rating = "PG";
// Update database
db.Movies.Update(selectedMovie);
db.SaveChanges();
}
}
}
private void deleteButton_Click(object sender, RoutedEventArgs e)
{
// Delete selected movie
if (movieListView.SelectedIndex > -1)
{
using (var db = new MovieTheaterContext())
{
// Delete selected movie from database
var selectedMovie = movieListView.SelectedItem as Movie;
db.Movies.Remove(selectedMovie);
db.SaveChanges();
// Remove from ListView
movieList.Remove(selectedMovie);
}
}
}