PRN212 Using EntityFramework and WPF Template
PRN212 Using EntityFramework and WPF Template
Use the Visual [Link] to create WPF application and Class Library (.dll) project.
Create a SQL Server database named MyStoreDB that has a Product table.
Apply Repository pattern in a project.
Add CRUD action methods to WPF application.
Run the project and test the WPF application actions.
1|Page
3. Database Design (MyStore)
Tạo CSDL từ script hoặc hình ảnh đã cho
Script:
GO
-- Nếu muốn tự tăng, đổi dòng dưới thành: ProductID INT IDENTITY(1,1) NOT NULL
);
GO
2|Page
(3 , N'Cà phê hạt Arabica 1kg', 2, 35, 210.000),
Table Products
3|Page
Step 01. Create a Blank solution.
Step 02. Create 4 Class Library projects.
Step 03. Create a WPF project.
Note:
Data Source in this case is the SQL Server Database
Services Project – This project represents a layer or component responsible for
implementing the business logic of an application.
Repository Project – This project provides an abstraction layer between the
application’s business logic and the underlying data source.
Data Access Layer Project – This project used to abstract and encapsulate the logic for
accessing data from a data source, such as a database.
4|Page
Activity 02: Write codes for the BusinessObjects project
Step 01. Install the following packages from NuGet:
[Link] --version 8.0.1
[Link] --version 8.0.1
[Link] --version 8.0.0
[Link]
Check the tool for EFCore (install/uninstall tool if needed) (dotnet SDK 8.0.202)
dotnet tool list --global
dotnet tool uninstall --global dotnet-ef (nếu dotnet-ef không trùng version với các
pakages m đã install)
dotnet tool install --global dotnet-ef --version 8.0.1
5|Page
Mở terminal đúng ngay tại đg dẫn của Bussiness
Step 01. On the DataAccessLayer project, add a class named [Link] and write codes
as follows:
namespace DataAccessLayer
{
public class ProductDAO
6|Page
{
public static List<Product> GetAll()
{
using var _db = new DbmotoRentalContext();
return _db.[Link](p => [Link]).ToList();
}
7|Page
}
8|Page
CÓ THỂ BỎ PHẦN TẠO [Link]
private static string? ReadConn()
.SetBasePath([Link])
.Build();
return [Link]("DBMotor");
if (![Link])
var cs = ReadConn();
if ([Link](cs))
[Link](cs);
9|Page
Activity 04: Write codes for the Repositories project
Step 01. On the Repositories project, add an interface named [Link] and write
codes as follows:
public interface IProductRepository
{
//void SaveProduct (Product c);
// void DeleteProduct(Product c);
//void UpdateProduct (Product c);
public List<Product> GetAll();
//Product GetProductById(int id);
}
10 | P a g e
public class ProductService
return _repository.GetAll();
using Service;
using Bussiness;
[Link]("Hello, World!");
[Link](product);
11 | P a g e
}
12 | P a g e
Step 01. On the WPF project, design UI as follows:
<Window x:Class="[Link]"
xmlns="[Link]
xmlns:x="[Link]
xmlns:d="[Link]
xmlns:mc="[Link]
xmlns:local="clr-namespace:WPFApp"
mc:Ignorable="d"
Loaded="Window_Loaded"
WindowStartupLocation="CenterScreen"
13 | P a g e
Title="Product Management" Height="670" Width="710">
<Grid>
<Grid>
<[Link]>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#0099FF" Offset="0"/>
<GradientStop Color="#FF347BDA" Offset="0.794"/>
<GradientStop Color="#FF60B1E7" Offset="1"/>
<GradientStop Color="#FF596FDD" Offset="0.309"/>
<GradientStop Color="#FF472FDE" Offset="0.484"/>
</LinearGradientBrush>
</[Link]>
<[Link]>
<RowDefinition Height="60"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</[Link]>
<[Link]>
<ColumnDefinition Width="119.415"/>
<ColumnDefinition Width="30.585"/>
<ColumnDefinition Width="47*"/>
<ColumnDefinition Width="513*"/>
</[Link]>
14 | P a g e
<TextBox x:Name="txtUnitsInStock" Margin ="4,4,4,4" [Link]="2"
[Link]="4" Text="" TextWrapping="Wrap" [Link]="2" />
</Grid>
</Grid>
</Window>
15 | P a g e
<Window x:Class="[Link]"
xmlns="[Link]
xmlns:x="[Link]
xmlns:d="[Link]
xmlns:mc="[Link]
xmlns:local="clr-namespace:WPFApp"
mc:Ignorable="d"
Title="LoginWindow" Height="450" Width="800"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
Background="Transparent"
AllowsTransparency="True">
<Grid>
<Border CornerRadius="10"
BorderThickness="2"
Opacity="0.95">
<[Link]>
<LinearGradientBrush StartPoint="0,0" EndPoint="1,1">
<GradientStop Color="#0099FF" Offset="0"/>
<GradientStop Color="#DA34AE" Offset="0.75"/>
<GradientStop Color="#FF60B1E7" Offset="1"/>
<GradientStop Color="#FF596FDD" Offset="0.309"/>
<GradientStop Color="#FF8C57CA" Offset="0.484"/>
</LinearGradientBrush>
</[Link]>
<[Link]>
<LinearGradientBrush StartPoint="0,1" EndPoint="1,0">
<GradientStop Color="#060531" Offset="0"/>
<GradientStop Color="#FF472FDE" Offset="1"/>
</LinearGradientBrush>
16 | P a g e
</[Link]>
<Grid>
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center"
Height="82" VerticalAlignment="Top" Width="632">
<TextBlock Text="LOGIN WINDOW"
Foreground="White"
FontSize="28"
FontWeight="Medium"
FontFamily="Montserrat"
Cursor="Hand"
Margin="180,30,0,0" Width="377"
/>
</StackPanel>
<StackPanel
Orientation="Vertical"
Margin="82,102,82,68">
<TextBlock Text="Username"
Foreground="DarkGray"
FontSize="12"
FontWeight="Medium"
FontFamily="Montserrat"
Margin="0,35,0,0"/>
<TextBox x:Name="txtUser"
FontSize="13"
FontWeight="Medium"
FontFamily="Montserrat"
Foreground="White"
CaretBrush="LightGray"
BorderBrush="DarkGray"
BorderThickness="0,0,0,2"
Height="28"
VerticalContentAlignment="Center"
Margin="0,5,0,0"
>
<[Link]>
<LinearGradientBrush></LinearGradientBrush>
</[Link]>
</TextBox>
<TextBlock Text="Password"
Foreground="DarkGray"
FontSize="12"
FontWeight="Medium"
FontFamily="Montserrat"
Margin="0,15,0,0"/>
<PasswordBox x:Name="txtPass"
FontSize="13"
FontWeight="Medium"
FontFamily="Montserrat"
Foreground="White"
17 | P a g e
CaretBrush="LightGray"
BorderBrush="DarkGray"
BorderThickness="0,0,0,2"
Height="28"
VerticalContentAlignment="Center"
Margin="0,5,0,0">
<[Link]>
<LinearGradientBrush></LinearGradientBrush>
</[Link]>
</PasswordBox>
<Button x:Name="btnLogin"
BorderThickness="0"
Content="LOG IN"
Foreground="White"
FontSize="12"
FontFamily="Montserrat"
Cursor="Hand"
Margin="0,50,0,0"
Click="btnLogin_Click">
<[Link]>
<Style TargetType="Button">
<Setter Property="Background" Value="#0099FF"/>
<[Link]>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#28AEED"/>
</Trigger>
</[Link]>
</Style>
</[Link]>
<[Link]>
<ControlTemplate TargetType="Button">
<Border Width="150" Height="40"
CornerRadius="20"
Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</[Link]>
</Button>
</StackPanel>
<StackPanel>
<Button x:Name="btnCancel"
BorderThickness="0"
Content="CANCEL"
Foreground="White"
FontSize="12"
FontFamily="Montserrat"
18 | P a g e
Cursor="Hand"
Margin="20,350,20,0"
Click="btnCancel_Click">
<[Link]>
<Style TargetType="Button">
<Setter Property="Background" Value="LightSeaGreen"/>
<[Link]>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="SeaGreen"/>
</Trigger>
</[Link]>
</Style>
</[Link]>
<[Link]>
<ControlTemplate TargetType="Button">
<Border Width="150" Height="40"
CornerRadius="20"
Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center"
HorizontalAlignment="Center"/>
</Border>
</ControlTemplate>
</[Link]>
</Button>
</StackPanel>
</Grid>
</Border>
</Grid>
</Window>
Step 02. Right-click on the project | Add | New Item, select JavaScript JSON Configuration
File then rename to [Link], click Add and write contents as follows:
{
"ConnectionStrings": {
"MyStockDB": "Server=(local);uid=sa;pwd=1234567890;database=MyStore;"
}
}
19 | P a g e
Step 04. Write codes for [Link]:
20 | P a g e
Step 05. Write codes for [Link]:
21 | P a g e
22 | P a g e
23 | P a g e
24 | P a g e
25 | P a g e
Step 06. Open [Link] and then update XAML code as follows:
<Application x:Class="[Link]"
xmlns="[Link]
xmlns:x="[Link]
xmlns:local="clr-namespace:WPFApp"
StartupUri="[Link]">
<[Link]>
</[Link]>
</Application>
Activity 05: Run the WPFApp project and test all actions
26 | P a g e