net entity framework
Post on 28-Oct-2014
139 Views
Preview:
TRANSCRIPT
Nguyễn NhuNguyễn Hoàng Thiện
1
Lập trình trên môi trường Windows
ADO.NET Entity FrameworkLập trình trên môi trường Windows
ADO.NET Entity Framework
ADO.NETADO.NETvar chuoiKetNoi = GetConnectionString(); var ketNoi = new SqlConnection(chuoiKetNoi); var boLenh = new SqlCommand(); boLenh.Connection = ketNoi; boLenh.CommandText = @"SELECT HoTen, MSSV FROM SinhVien"; var boDoc = boLenh.ExecuteReader(); while (boDoc.Read()) {
Console.WriteLine("{0} {1}", boDoc["HoTen"], boDoc["MSSV"]); }
2
Object-Relational Mapping (ORM)Object-Relational Mapping (ORM)• Là một phương pháp lập trình để
chuyển đổi từ mô hình database sang mô hình đối tượng.
• Developer chỉ cần quan tâm tới việc ánh xạ các đối tượng sang CSDL
3
Object-Relational Mapping (ORM)Object-Relational Mapping (ORM)
4
Entity Framework là gì?Entity Framework là gì?• ADO.NET Entity Framework là một
Object/Relational Mapping (ORM) framework và là một bộ công nghệ thuộc ADO.NET dành cho việc phát triển các ứng dụng tương tác với dữ liệu.
• Cho phép làm việc với dữ liệu quan hệ như là các objects, loại bỏ hầu hết các code cho data access phải viết. Sử dụng Entity Framework, có thể sử dụng và khai thác sức mạnh của LINQ trong việc khai thác dữ liệu
5
Mô hình Entity FrameworkMô hình Entity Framework
6
EntityEntity
7
• Enity là một đối tượng được ánh xạ từ một bảng trong cơ sở dữ liệu lên bộ nhớ
EntityEntityEntity vs ObjectGiống nhau:•Entity có thể hiểu là một kiểu.•Entity có properties.•Những properties này của entity có thể giữ tham chiếu đến những entity khác.•Mỗi entity có một đặc tính riêng biệt.
Khác nhau:•Entity tồn tại trong một collection.•Mỗi enity có những associations với những entity khác.•Entity có primary key để nhận biết một unique entity.
8
EntityEntityEntity vs Relational dataGiống nhau:•Entity tồn tại trong entity set.•Entity có relationships với những entity khác.
•Mỗi Entity có một primary key.Khác nhau:•Entity hỗ trợ complex types.•Entity hỗ trợ inheritance.•Entity không có physical storage knowledge.
9
Entity Framework 4.0 FeaturesEntity Framework 4.0 Features• Hổ trợ POCO• Hổ trợ Model-First • Deferred Loading of Relational
Objects• Hổ trợ LINQ to Entities Functions.• Quy tắc đặt tên• Hổ trợ kiểu phức (complex)• Cải tiến Model Browser
10
Hổ trợ các HQTCSDLHổ trợ các HQTCSDL• Oracle • MySql • PostgreSQL • SQL Anywhere • DB2 • Informix • U2 • Ingres • Progress • Firebird • Synergy • Virtuoso
11
The Entity Data Model The Entity Data Model
• Entity Data Model (EDM) là cầu nối giữa ứng dụng và dữ liệu, và là thành phần cho phép làm việc với dữ liệu một cách nhận thức hơn là làm việc trực tiếp với cơ sở dữ liệu.
12
The Entity Data Model The Entity Data Model
• Tạo ra một EDM Database-first: Tạo ra một EDM từ
database có sẳn. Model-first: Tự định nghĩa model và tạo
ra database, mappings, and classes từ model tự định nghĩa này.
Code-only: Sử dụng Plain Old CLR Objects (POCO) entites và không cần dùng file *.EDMX.
13
The Entity Data Model The Entity Data Model
• DEMO – Database-first– Model-first
14
The Entity Data Model Inside and Out The Entity Data Model Inside and Out
• The Designer Window and the EDM • Entites• Mapping Details• The EDM Model Parts• EDM-Generated Classes
15
The Entity Data Model Inside and Out The Entity Data Model Inside and Out
• The Designer Window and the EDM – The Designer Window– Model Browser Window– Mapping Details Window
16
The Entity Data Model Inside and Out The Entity Data Model Inside and Out
• Entites– Scalar Properties– Complex Types– Foreign Keys and Relationships
(Associations)– Navigation Properties
17
The Entity Data Model Inside and Out The Entity Data Model Inside and Out
• Một EDM có 3 layers– Conceptual layer(CSDL)– Mapping layer(MSL)– Storage layer (SSDL)
18
The Entity Data Model Inside and OutThe Entity Data Model Inside and Out
• EDM-Generated Classes
19
Truy Vấn EDMTruy Vấn EDM
• Truy vấn trong Entity Framework là truy vấn trên đối tượng, không phải là truy vấn trực tiếp trong cơ sở dữ liệu.
• Các cú pháp truy vấn:– Cú pháp Query-Expression– Cú pháp Method-Based
20
Truy Vấn EDM Truy Vấn EDM • Query-Expression Syntax
• Method-Based Syntax • Thường sử dụng Lamba Expression làm tham
số.
21
var result = from stu in context.Students where stu.Name == "Nguyen Nhu" orderby stu.Name select stu;
var result = from stu in context.Students where stu.Name == "Nguyen Nhu" orderby stu.Name select stu;
var result = context .Students
.Where(stu => stu.Name == "Nguyen Nhu") .OrderBy(i => i.Name);
var result = context .Students
.Where(stu => stu.Name == "Nguyen Nhu") .OrderBy(i => i.Name);
Truy Vấn EDMTruy Vấn EDM
• Tùy chọn truy vấn– LINQ to Entities– Entity SQL
22
Truy Vấn EDMTruy Vấn EDM
• LINQ to Entities
23
var result = from stu in context.Students where stu.ID == 1 || stu.Name.StartsWith("Nguyen") orderby stu.ID, stu.Name descending select new { ID = stu.ID, Name = stu.Name };
var result = from stu in context.Students where stu.ID == 1 || stu.Name.StartsWith("Nguyen") orderby stu.ID, stu.Name descending select new { ID = stu.ID, Name = stu.Name };
Truy Vấn EDMTruy Vấn EDM
• Entity SQL
24
var str = "SELECT VALUE stu FROM DemoEntityContainer.Students as stu WHERE stu.ID = 1"; var lst = context.CreateQuery<Student>(str); foreach (var i in lst) { MessageBox.Show(i.Name); }
var str = "SELECT VALUE stu FROM DemoEntityContainer.Students as stu WHERE stu.ID = 1"; var lst = context.CreateQuery<Student>(str); foreach (var i in lst) { MessageBox.Show(i.Name); }
Làm việc với Entities Làm việc với Entities
25
Làm việc với Entities Làm việc với Entities
• ObjectContext– Là đối tượng chủ chốt trong Entity
Framework – Quản lý tất cả các thao tác CRUD xuống
database
26
var context = new DemoEntityContainer(); var lstStudent = context.Students; var lstClass = context.Classes; context.AddToStudents(new Student()); context.Students.DeleteObject(new Student());
var context = new DemoEntityContainer(); var lstStudent = context.Students; var lstClass = context.Classes; context.AddToStudents(new Student()); context.Students.DeleteObject(new Student());
Làm việc với Entities Làm việc với Entities
• ObjectStateEntryMỗi entiy sẽ có một thuộc tính EntityState có các giá trị:–Detached: object tồn tại, nhưng không bị theo dõi bởi Object Services.–Unchanged: object chưa được chỉnh sửa từ khi nó được load vào context hay từ lần cuối cùng phương thức SaveChanges được gọi.–Added: object vừa được thêm vào context, và phương thức SaveChanges chưa được gọi.–Deleted: object bị xóa khỏi context.–Modified: object đã thay đổi, nhưng phương thức SaveChanges chưa được gọi.
27
Làm việc với Entities Làm việc với Entities
• Saving Changes– Cập nhật tất cả những thay đổi từ entity
xuống database.– Thực hiện qua phương thức
SaveChange()• SaveChange()• SaveChange(SaveOptions)
28
Làm việc với Entities Làm việc với Entities
• Add Entity
29
using (var context = new DemoEntityContainer()) {
var student = new Student(); student.StudentCode = "08!628$%4"; student.Name = "Mai Van Toan"; student.GPA = %; student.DateOfBirth = new DateTime($990, $0, $0); student.ClassID = 2; student.Gender = true; context.Students.AddObject(student); // Cập nhật thay đổi xuống database context.SaveChanges();
}
using (var context = new DemoEntityContainer()) {
var student = new Student(); student.StudentCode = "08!628$%4"; student.Name = "Mai Van Toan"; student.GPA = %; student.DateOfBirth = new DateTime($990, $0, $0); student.ClassID = 2; student.Gender = true; context.Students.AddObject(student); // Cập nhật thay đổi xuống database context.SaveChanges();
}
Làm việc với Entities Làm việc với Entities
• Update Entity
30
using (var context = new DemoEntityContainer()) {
var student = (from stu in context.Students where stu.ID == $ select stu).FirstOrDefault(); student.Name = "Nguyen Nhu"; student.GPA = 10; // Cập nhật thay đổi xuống database context.SaveChanges();
}
using (var context = new DemoEntityContainer()) {
var student = (from stu in context.Students where stu.ID == $ select stu).FirstOrDefault(); student.Name = "Nguyen Nhu"; student.GPA = 10; // Cập nhật thay đổi xuống database context.SaveChanges();
}
Làm việc với Entities Làm việc với Entities
• Delete Entity
31
using (var context = new DemoEntityContainer()) {
var student = (from stu in context.Students where stu.ID == 3 select stu).FirstOrDefault(); context.Students.DeleteObject(student); // Cập nhật thay đổi xuống database context.SaveChanges();
}
using (var context = new DemoEntityContainer()) {
var student = (from stu in context.Students where stu.ID == 3 select stu).FirstOrDefault(); context.Students.DeleteObject(student); // Cập nhật thay đổi xuống database context.SaveChanges();
}
Stored ProceduresStored Procedures
• Cho phép Overide lại toán tử CUD bằng những Stored Procedures tự định nghĩa.
• Sử dụng Function(Stored Procedures) trong truy vấn trên đối tượng.
32
top related