net entity framework

of 32 /32
Nguyễn Nhu Nguyễn Hoàng Thiện 1 Lập trình trên môi trường Windows ADO.NET Entity Framework

Author: traphuoctuan

Post on 28-Oct-2014

119 views

Category:

Documents


0 download

Embed Size (px)

TRANSCRIPT

Lp trnh trn mi trng Windows

ADO.NET Entity Framework

Nguyn Nhu Nguyn Hong Thin

1

ADO.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) L mt phng php lp trnh chuyn i t m hnh database sang m hnh i tng. Developer ch cn quan tm ti vic nh x cc i tng sang CSDL

3

Object-Relational Mapping (ORM)

4

Entity Framework l g? ADO.NET Entity Framework l mt Object/Relational Mapping (ORM) framework v l mt b cng ngh thuc ADO.NET dnh cho vic pht trin cc ng dng tng tc vi d liu. Cho php lm vic vi d liu quan h nh l cc objects, loi b hu ht cc code cho data access phi vit. S dng Entity Framework, c th s dng v khai thc sc mnh ca LINQ trong vic khai thc d 5 liu

M hnh Entity Framework

6

Entity Enity l mt i tng c nh x t mt bng trong c s d liu ln b nh

7

EntityEntity vs Object Ging nhau:Entity c th hiu l mt kiu. Entity c properties. Nhng properties ny ca entity c th gi tham chiu n nhng entity khc. Mi entity c mt c tnh ring bit.

Khc nhau:Entity tn ti trong mt collection. Mi enity c nhng associations vi nhng entity khc. Entity c primary key nhn bit mt unique entity.8

EntityEntity vs Relational data Ging nhau:Entity tn ti trong entity set. Entity c relationships vi nhng entity khc.

Mi Entity c mt primary key.

Khc nhau:Entity h tr complex types. Entity h tr inheritance. Entity khng c physical storage knowledge.9

Entity Framework 4.0 Features H tr POCO H tr Model-First Deferred Loading of Relational Objects H tr LINQ to Entities Functions. Quy tc t tn H tr kiu phc (complex) Ci tin Model Browser10

H tr cc HQTCSDL Oracle MySql PostgreSQL SQL Anywhere DB2 Informix U2 Ingres Progress Firebird Synergy Virtuoso

11

The Entity Data Model Entity Data Model (EDM) l cu ni gia ng dng v d liu, v l thnh phn cho php lm vic vi d liu mt cch nhn thc hn l lm vic trc tip vi c s d liu.

12

The Entity Data Model To ra mt EDM Database-first: To ra mt EDM t database c sn. Model-first: T nh ngha model v to ra database, mappings, and classes t model t nh ngha ny. Code-only: S dng Plain Old CLR Objects (POCO) entites v khng cn dng file *.EDMX.13

The Entity Data Model DEMO Database-first Model-first

14

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 Designer Window and the EDM The Designer Window Model Browser Window Mapping Details Window

16

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 Mt EDM c 3 layers Conceptual layer(CSDL) Mapping layer(MSL) Storage layer (SSDL)

18

The Entity Data Model Inside and Out EDM-Generated Classes

19

Truy Vn EDM Truy vn trong Entity Framework l truy vn trn i tng, khng phi l truy vn trc tip trong c s d liu. Cc c php truy vn: C php Query-Expression C php Method-Based

20

Truy Vn EDM Query-Expression Syntaxvar result = from stu in context.Students where stu.Name == "Nguyen Nhu" orderby stu.Name select stu;

Method-Based Syntax Thng s dng Lamba Expression lm tham s.var result = context .Students .Where(stu => stu.Name == "Nguyen Nhu") .OrderBy(i => i.Name);21

Truy Vn EDM Ty chn truy vn LINQ to Entities Entity SQL

22

Truy Vn EDM LINQ to Entities

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 };

23

Truy Vn EDM Entity SQLvar str = "SELECT VALUE stu FROM DemoEntityContainer.Students as stu WHERE stu.ID = 1"; var lst = context.CreateQuery(str); foreach (var i in lst) { MessageBox.Show(i.Name); }

24

Lm vic vi Entities

25

Lm vic vi Entities ObjectContext L i tng ch cht trong Entity Framework Qun l tt c cc thao tc CRUD xung databasevar context = new DemoEntityContainer(); var lstStudent = context.Students; var lstClass = context.Classes; context.AddToStudents(new Student()); context.Students.DeleteObject(new Student());26

Lm vic vi Entities ObjectStateEntryMi entiy s c mt thuc tnh EntityState c cc gi tr:Detached: object tn ti, nhng khng b theo di bi Object Services. Unchanged: object cha c chnh sa t khi n c load vo context hay t ln cui cng phng thc SaveChanges c gi. Added: object va c thm vo context, v phng thc SaveChanges cha c gi. Deleted: object b xa khi context. Modified: object thay i, nhng phng thc SaveChanges cha c gi.27

Lm vic vi Entities Saving Changes Cp nht tt c nhng thay i t entity xung database. Thc hin qua phng thc SaveChange() SaveChange() SaveChange(SaveOptions)

28

Lm vic vi Entities Add Entityusing (var context = new DemoEntityContainer()) { var student = new Student(); student.StudentCode = "0862154"; student.Name = "Mai Van Toan"; student.GPA = 5; student.DateOfBirth = new DateTime(1990, 10, 10); student.ClassID = 2; student.Gender = true; context.Students.AddObject(student); // Cp nht thay i xung database context.SaveChanges(); }

29

Lm vic vi Entities Update Entityusing (var context = new DemoEntityContainer()) { var student = (from stu in context.Students where stu.ID == 1 select stu).FirstOrDefault(); student.Name = "Nguyen Nhu"; student.GPA = 10; // Cp nht thay i xung database context.SaveChanges(); }30

Lm vic vi Entities Delete Entityusing (var context = new DemoEntityContainer()) { var student = (from stu in context.Students where stu.ID == 3 select stu).FirstOrDefault(); context.Students.DeleteObject(student); // Cp nht thay i xung database context.SaveChanges();

}31

Stored Procedures Cho php Overide li ton t CUD bng nhng Stored Procedures t nh ngha. S dng Function(Stored Procedures) trong truy vn trn i tng.

32