PRN212_Lab_01_AutomobileManagement_Using_EntityFramework and WPF
PRN212_Lab_01_AutomobileManagement_Using_EntityFramework and WPF
Introduction
Imagine you're an employee of a car retailer named Automobile Store. Your
manager has asked you to develop a WPF application for automobile
management (CarID, CarName, Manufacturer, Price, and ReleasedYear). The
application has to support adding, viewing, modifying, and removing products—a
standardized usage action verbs better known as Create, Read, Update, Delete
(CRUD).
This lab explores creating an application using WPF with .NET Core, and C#. An
SQL Server Database will be created to persist the car's data that will be used
for reading and managing automobile data by Entity Framework Core
Lab Objectives
In this lab, you will:
▪ Use the Visual Studio.NET to create WPF application and Class Library
(.dll) project.
▪ Create a SQL Server database named MyStock that has a Cars table.
▪ Develop a DataProvider class to perform CRUD actions using Entity
Framework Core.
▪ Apply Dependency injection (DI) in WPF application.
▪ Apply Repository pattern and Singleton pattern in a project.
▪ Add CRUD action methods to WPF application.
▪ Run the project and test the WPF application actions.
1|Page
2|Page
Fullname_MyStock Database
3|Page
1
4|Page
Step 02. Create a new Class Library project named
No_Fullname_Lab01_AutomobileLibrary .
• From the File menu | Add | New Project, on the Add New Project dialog,
select “Class Library” and performs steps as follows:
Step 02. Create folders and add classes to the project as follows:
5|Page
Step 03. Right-click on project , select Open In Terminal. On Developer
PowerShell dialog execute the following commands to generate model:
By following these steps, you can generate model classes for your Entity
Framework Core project using the Developer PowerShell dialog.
Remember to replace "YourConnectionString" with your actual database
connection string and adjust other options as needed based on your
project's requirements and database provider.
6|Page
Step 04.
On the DataAccess folder, add a class named CarManagement.cs and
write codes as follows:
7|Page
8|Page
Step 05. Write codes for ICarRepository.cs as follows:
9|Page
Activity 03: Design UI and write codes for
AutomobileWPFApp project
Step 01.
On the AutomobileWPFApp project, rename MainWindow.xaml to
WindowCarManagement.xaml and then design UI as follows:
10 | P a g e
ListView Control
11 | P a g e
<Label Name="lbTitle" Foreground="Red" FontWeight="DemiBold"
FontSize="20" Content="Car Information" />
12 | P a g e
DisplayMemberBinding="{Binding Path=CarName}"/>
<GridViewColumn Header="Manufacturer" Width="200"
DisplayMemberBinding="{Binding Path=Manufacturer }"/>
<GridViewColumn Header="Price" Width="100"
DisplayMemberBinding="{Binding Path=Price,
StringFormat={}{0:N3}}"/>
<GridViewColumn Header="ReleasedYear" Width="100"
DisplayMemberBinding="{Binding Path=ReleasedYear}"/>
</GridView>
</ListView.View>
</ListView>
<! Button control-->
<Button Grid.Row="3" x:Name="btnClose" Margin="10"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Width="80" Content="Close" Click="btnClose_Click" />
</Grid>
</DockPanel>
</Grid>
</Window>
Step 02. Right-click on the project | Add | New Item, select JavaScript
JSON Configuration File then rename to appsettings.json, click Add and
write contents as follows:
13 | P a g e
1
1
14 | P a g e
Step 07. Open App.xaml and then update XAML code as follows:
15 | P a g e
Activity 05: Run the AutomobileWPFApp project
and test all actions
Step 01. Click the Load button and display the result as the below figure.
16 | P a g e
Step 02. Enter the values on TextBoxes then click the Insert button to add
a new car.
Step 03. Select a row on the ListView then click the Delete button to
remove a Car.
Step 04. Click a row on the ListView and edit the values on TextBoxes,
then click the Update button to update the car information.
17 | P a g e