Chương 5 - Entity Framework
Chương 5 - Entity Framework
Chương 5 - Entity Framework
Introduction
What is Entity Framework Core?
Entity Framework Core is the new version of Entity Framework
after EF 6.x. It is open-source, lightweight, extensible and a cross-
platform version of Entity Framework data access technology.
EF Core is an object-relational mapper (ORM). Object-relational
mapping is a technique that enables developers to work with data
in object-oriented way by performing the work required to map
between objects defined in an application's programming
language and data stored in relational datasources.
EF Core is intended to be used with .NET Core applications.
However, it can also be used with standard .NET 4.5+ framework
based applications.
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Introduction
What is Entity Framework Core?
The following figure illustrates the supported application
types, .NET Frameworks and OSs.
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Introduction
Why use an ORM?
Most development frameworks include libraries that enable access
to data from relational databases via recordset-like data
structures. The following code sample illustrates a typical scenario
where data is retrieved from a database and stored in an ADO.NET
DataTable so that it is accessible to the program's code:
Introduction
Why use an ORM?
The data within the DataTable is accessible via numeric or string
indexers and needs to be converted from object to the correct
type:
foreach(DataRow row in dt.Rows)
{
int productId = Convert.ToInt32(row[0]);
string productName = row["ProductName"].ToString();
}
This late-bound or "weakly-typed" approach to data access is
prone to error. Problems commonly arise from mistyping the
name of a column, or finding that the name of the column has
been changed in the database, or from a change to the order in
which fields are specified in the SQL statement without a
corresponding change being made to the application code
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Introduction
Why use an ORM?
Strong Typing
• When you take a strongly-typed approach to data, you work with
properties of predefined classes that form a domain model in an
object-oriented way:
public class Product
{
int ProductId { get; set; }
string ProductName { get; set; }
}
int productId = myProduct.ProductId;
string productName = myProduct.ProductName;
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Introduction
Why use an ORM?
ORMs are pre-written libraries of code that do this work for you.
Full-featured ORMs do a lot more too. They can:
• map a domain model to database objects
• create databases and maintain the schema in line with changes
to the model
• generate SQL and execute it against the database
• manage transactions
• keep track of objects that have already been retrieved
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Installation
Install Entity Framework Core
EF Core is not a part of .NET Core and standard .NET framework. It
is available as a NuGet package. You need to install NuGet
packages for the following two things to use EF Core in your
application:
• EF Core DB provider
• EF Core tools
Install EF Core DB Provider
• EF Core allows us to access databases via the provider model.
There are different EF Core DB providers available for the
different databases. These providers are available as NuGet
packages.
• Here, we want to access MS SQL Server database, so we need to
install Microsoft.EntityFrameworkCore.SqlServer NuGet
package.
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Installation
Install Entity Framework Core
Install EF Core DB Provider
• To install the DB provider NuGet package, right click on the
project in the Solution Explorer in Visual Studio and select
Manage NuGet Packages.. (or select on the menu: Tools -> NuGet
Package Manager -> Manage NuGet Packages For Solution)
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Installation
Install Entity Framework Core
Install EF Core DB Provider
• Notice that the provider NuGet package also installed other
dependent packages such as
Microsoft.EntityFrameworkCore.Relational and
System.Data.SqlClient.
• Alternatively, you can also install provider's NuGet package using
Package Manager Console. Go to Tools -> NuGet Package Manager
-> Package Manager Console and execute the following command
to install SQL Server provider package:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Installation
Install Entity Framework Core
Install EF Core Tools
• The command-line interface (CLI) tools for Entity Framework Core
perform design-time development tasks. For example, they create
migrations, apply migrations, and generate code for a model
based on an existing database. The commands are an extension
to the cross-platform dotnet command, which is part of the .NET
Core SDK. These tools work with .NET Core projects.
• EF Tools are available as NuGet packages. You can install NuGet
package for EF tools depending on where you want to execute
commands: using dotnet CLI (Command line interface).
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Installation
Install Entity Framework Core
Install EF Core Tools
• The command-line interface (CLI) tools for Entity Framework Core
perform design-time development tasks. For example, they create
migrations, apply migrations, and generate code for a model
based on an existing database. The commands are an extension
to the cross-platform dotnet command, which is part of the .NET
Core SDK. These tools work with .NET Core projects.
• EF Tools are available as NuGet packages. You can install NuGet
package for EF tools depending on where you want to execute
commands: using dotnet CLI (Command line interface).
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Installation
Install Entity Framework Core
Install EF Core Tools
• dotnet ef can be installed as either a global or local tool. Most
developers prefer installing dotnet ef as a global tool using the
following command:
dotnet tool install --global dotnet-ef
• Update the tool using the following command:
dotnet tool update --global dotnet-ef
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
builder.Services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=DemoManagerStudent;Integrated
Security=True;”});
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
services.AddDbContext<ApplicationDbContext>(options => {
options.UseSqlServer(builder.Configuration.GetConnectionString("MyConnection"));
});
Trường đại học xây dựng Hà Nội
Bộ môn Khoa học Máy tính
Bài tập
Sửa lại project quản lý sinh viên
Tạo migration và update vào database 3 bảng student, classroom và
student classroom
Thực hiện việc xử lý logic thay vì dùng List chuyển sang sử dụng
DbContext theo hướng dẫn