So sánh entity framework core và ado net
EF Core là framework (thư viện khung) để ánh xạ các đơn vị dữ liệu mô tả bằng lớp (đối tượng) vào cơ sở dữ liệu quan hệ, nó cho phép ánh xạ vào các bảng CSDL, tạo CSDL, truy vấn với LINQ, tạo và cập nhật vào database. Show
Để sử dụng using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
2 hãy thêm những package cần thiết vào, chạy các lệnh sau:dotnet add package System.Data.SqlClient dotnet add package Microsoft.EntityFrameworkCore dotnet add package Microsoft.EntityFrameworkCore.SqlServer dotnet add package Microsoft.EntityFrameworkCore.Design dotnet add package Microsoft.Extensions.DependencyInjection dotnet add package Microsoft.Extensions.Logging dotnet add package Microsoft.Extensions.Logging.Console Những namespace có thể dùng: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; Tạo Model đơn giản, ánh xạ bảng CSDLModel là mô hình hóa các đối tượng dữ liệu trong hệ quản trị CSDL thành các đối tượng lập trình, đó là các lớp (class) tương ứng với các bảng ... Hãy tạo ra một dự án using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
3 trong thư mục using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
4 có cài đặt các package trên để thực hành. Ở đây tạo ra một model đơn giản, lớp có tên using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
5 biểu diễn các dòng trong bảng của CDSL, bảng này tên là using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
6Trước tiên, lớp này định nghĩa thuần túy gồm các thuộc tính như sau: using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
Trước khi sử dụng model using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
5 trong EF Core, hãy bổ sung các thiết lập thông qua các Attribute (sử dụng Sử dụng Attribute) như sau:
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }Đây là khai báo một lớp bình thường, chỉ có bổ sung thêm vài thuộc tính mô tả (Attribute) cho lớp, thuộc tính - các Attribute này được sử dụng bởi EF. Tạo Context - DbContextDbContext trong EF là ngữ cảnh làm việc, nó biểu diễn, chứa các thông tin cần thiết của một phiên làm việc với CSDL. Để thực hiện tạo ra mối liên hệ bảng Products tong CSDL và model, tạo ra Context như sau: Tạo lớp kế thừa using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }4 đặt tên là using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }5, lớp này mang ý nghĩa như là một CSDL. Trong đó cần nạp chồng using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }6 để cấu hình (thiết lập chuỗi kết nối ...), và tạo ra thuộc tính có kiểu using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }7 chính là bảng trong CSDL using Microsoft.EntityFrameworkCore; namespace ef01 { }
Từ lớp kế thừa using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }4 là using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }5 có thể tương tác với CSDL bằng cách gọi các phương thức thích hợp trong DbContext, tìm hiểu một số trường hợp đơn giản sau đây. Tạo và xóa Databaseusing System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }4 có thuộc tính using Microsoft.EntityFrameworkCore; namespace ef01 { }
1, thuộc tính này là đối tượng kiểu using Microsoft.EntityFrameworkCore; namespace ef01 { }
2, từ đó có thể tạo / xóa database.EnsureCreatedAsync là phươnng thức của using Microsoft.EntityFrameworkCore; namespace ef01 { }
2 để tạo ra database, nếu database nó đang không tồn tại, nếu DB đã tồn tại thì không làm gì cả.// Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
Chạy thử phương thức trong hàm Mainstatic async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
Thi hành thì nó sẽ tạo ra database, nếu DB đó chưa có! Database này tạo ra có tên using Microsoft.EntityFrameworkCore; namespace ef01 { }
4, có một bảng tên using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
6 với cấu trúc giống mô tả trong model using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
5DeleteDatabase là phươnng thức của using Microsoft.EntityFrameworkCore; namespace ef01 { }
2 để xóa CSDL.public static async Task DeleteDatabase() { }
Phương thức using Microsoft.EntityFrameworkCore; namespace ef01 { }
8, using Microsoft.EntityFrameworkCore; namespace ef01 { }
9 dùng kỹ thuật // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
0, nếu muốn dùng phiên bản đồng bộ thì là// Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
1, // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
2Chèn dữ liệu vào các bảng, AddAsync, AddRangeAsync, SaveChangesAsyncCác đối tượng using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }4 hay // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
4 (như thuộc tính products của lớp ProductContext ở trên) có phương thức // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
5 để bạn chèn đối tượng phù hợp vào DbContext, nó nhận tham số là đối tượng Model cần chèn vào. Sau đó gọi phương thức // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
6 để thực hiện cập nhật dữ liệu vào Server SQL// Thực hiện chèn hai dòng dữ liệu vào bảng Product // Dùng AddAsync trong DbSet và trong DbContext public static async Task InsertProduct() { }
Chạy thử:static async Task Main(string[] args) { }
Nếu muốn thêm một lúc nhiều dữ liệu thì dùng // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
7, nó có thể nhận đối số là mảng các đối tượng cần chèn vàousing Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 0 Đọc dữ liệu từ bảng, truy vấn với LINQCác đối tượng // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
4 có phương thức // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
9 hay static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
0 để lấy về tất cả các dữ liệu (List) của bảng. Đặc biệt bạn có thể dùng LINQ ( đọc Linq C#) trên các đối tượng này (nguồn là các DbSet)using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 1 Cập nhật dữ liệu trong EFMuốn cập nhật dữ liệu, chỉ việc thay đổi thuộc tính của đối tượng đọc được, sau đó gọi static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
1using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 2 Nếu muốn một đối tượng riêng lẻ, không giám sát - muốn thực hiện cập nhật thì gọi đến phương thức Update của DbSet using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 3 Nếu muốn cập nhật một số trường nào đó, dùng cách cập nhật đối tượng độc lập using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 4 Lưu ý, các đối tượng như Product kết quả truy vấn, hoặc được thêm vào ... thì mặc định là được giám sát - theo dõi bởi EF. Khi những đối tượng này thay đổi trạng thái thì static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
2 sẽ thực hiện các tác vụ dựa theo trạng thái của nó.Mỗi dòng Product trả về từ truy vấn, hoặc thêm vào thì trong EF có một đối tượng static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
3 tương ứng để để quản lý. Để lấy EntityEntry có thể dùng phương thức Entry, bạn có thể dùng nó để thay đổi trạng thái thủ công, ví dụ để điều chỉnh sự ảnh hưởng khi gọi SaveChange. Ví dụ, loại bỏ không bị giảm sát bởi EF cho đối tượng product nhận đượcusing Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 5 Có một số state gồm: static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
4, static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
5, static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
6, static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
7, static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
8Xóa dữ liệu trong EFĐể xóa dữ liệu khỏi DB, chỉ việc yêu cầu xóa đối tượng khỏi DbContext bằng phương thức static async Task Main(string[] args) { }
// Tạo mydata
// CSDL mydata : tạo thành công
9, rồi gọi // Tạo Database mydata (tên mydata từ thông tin kết nối) // Gồm tất cả các bảng định nghĩa bởi các thuộc tính kiểu DbSet public static async Task CreateDatabase() { }
6 để cập nhậtusing Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 6 EF Logger hiện thị SQL Query trên terminalNếu muốn thật sự các câu truy vấn do EF, Linq sinh ra để tương tác với Database thì thêm loger như sau, thêm vào lớp ProductContext phương thức SetLogging: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 7 Mã nguồn - Source codeEF Logger trong .Net Core 3.xĐối với .Net Core 3.x để thiết lập Logger in thông tin tại console thì cần tạo ra một public static async Task DeleteDatabase() { }
1, sau đó thiết lập sử dụng nó ở phương thức using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }6 của lớp using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }5 Đầu tiên hãy đảm bảo cài đặt các Package mới nhất: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 8 Tạo ILoggerFactory như sau: using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Diagnostics; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; 9 Mặc định ILoggerFactory này bắt tất cả các sự kiện, nếu muốn thiết lập các category muốn lọc cũng như cấp độ thì gọi thêm phương thức public static async Task DeleteDatabase() { }
4 khi tạo public static async Task DeleteDatabase() { }
1, ví dụ:using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; namespace ef01 { }
0Trường hợp trên thiết lập lọc các log thuộc chuyên mục public static async Task DeleteDatabase() { }
6 ở mức độ public static async Task DeleteDatabase() { }
7 và chuyên mục public static async Task DeleteDatabase() { }
8 ở mức độ public static async Task DeleteDatabase() { }
9Các cấp khác xem tại: LogLevel Các tên category tham khảo đối với EF Logger: DbLoggerCategory.Database.Command, DbLoggerCategory.Database.Connection, DbLoggerCategory.Database.Transaction, DbLoggerCategory.Infrastructure, DbLoggerCategory.Migration, DbLoggerCategory.Model, DbLoggerCategory.Query, DbLoggerCategory.Scaffolding, DbLoggerCategory.Update |