ADO NET Entity Framework

66
09 Gil Fink. All rights reserved. © Copyright SELA software & Education Labs Ltd. 14-18 Baruch Hirsch St.Bnei Brak 51202 Israel www.sela.co.il

description

desc

Transcript of ADO NET Entity Framework

2009 Gil Fink. All rights reserved. Copyright SELA software & Education Labs Ltd. 14-18 aruch !irsch St.nei ra" #1$%$ &srae'www.se'a.co.i' 2009 Gil Fink. All rights reserved. Entity Framework IntroductionExploring the Entity Data ModelQuerying and Manipulating Entity Data ModelsCustomizing Entity Data Models ExamplesEF4Summary 2009 Gil Fink. All rights reserved. he !Impedance Mismatch" Relational Database Conceptual / Business Model (Objects) 2009 Gil Fink. All rights reserved. Data access #ramework Supports data$centric applications and ser%ices Ena&les programming against a conceptual application model Ena&les independency o# any data storage engine or relational schema 2009 Gil Fink. All rights reserved. EF uses a model called an Entity Data Model 'EDM( EDM is a client$side data model EDM is an a&straction layer on top o# the data storage )emo%e the pain o# Interacting with the data storage ranslating the data into o&*ects 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Items descri&ed in the EDM are called entities Entities ha%e only properties &ut no &eha%ior Entities can ha%e relationships with other entities 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. he model doesn+t ha%e any knowledge o# the data storage he &ackend data storage has no impact on your model or your code ,ses a pro%ider model to interact with the data storage -%aila&le pro%iders. SQ/ Ser%er 0racle MySQ/ Many more 2009 Gil Fink. All rights reserved. Entity Framework IntroductionExploring the Entity Data ModelQuerying and Manipulating Entity Data ModelsCustomizing Entity Data Models ExamplesEF4Summary 2009 Gil Fink. All rights reserved. -utomatically generates classes #rom the model akes care o# all o# the data&ase connecti%ity 1ro%ides common 2uery syntax #or 2uerying the model 1ro%ides a mechanism #or tracking changes to the model+s o&*ects 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Designer 3indow. 4raphical representation o# an EDM and its mem&ers Ena&les adding more #eatures to the model Ena&les properties con5guration Ena&les updating #rom the data store Ena&les model %alidation 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. he hree 1arts o# the Model.he image is taken #rom 6ulia /erman7s &ook 1rogramming Entity Framework8 9st Edition 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. EF automatically creates a set o# classes #rom the model :ou work with the model through the generated classes E%ery change to the model can change the generated classes 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Entity Framework IntroductionExploring the Entity Data ModelQuerying and Manipulating Entity Data ModelsCustomizing Entity Data Models ExamplesEF4Summary 2009 Gil Fink. All rights reserved. Queries are &uilt against a data model EDM 2uery trans#orm into data storage 2uery Query results materialize into model entitiesThe image is taken from ulia !erman"s book#rogramming $ntit% &rame'ork( )st $dition 2009 Gil Fink. All rights reserved. hree kinds o# 2ueries in EF /I;Q to Entities Entity SQ/ with 0&*ect Ser%ices Entity SQ/ with Entity Client E%ery kind has its pros and cons 2009 Gil Fink. All rights reserved. Queries written in /I;Q syntaxSupport #or /I;Q #eaturesFull IntelliSense supportvar courses = from course in context.Courseswhere course.Title.StartsWith("C")orderby course.Title ascendingselect new} Title = course.Title !ocation = course.!ocation"# 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. $SQ/$like 2uery language1ro%ide the necessary capa&ilities #or 2uerying the EDMEF translates Entity SQ/ into storage$speci5c 2ueriesvar qStr = @"SELECT VALUE c FROM SchoolEntities.Courses AS c !ERE c.Title="Calculus#"$ var courses = conte%t.Create&uer'(Course)*qStr+$ 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Streams data &ack to the application )esem&les S2lClient8 0racleClient and the other client pro%idersusin, *var conn = ne- Entit'Connection*"na.e=/ro,ra..in,EF012Entities"++ 3 conn.O4en*+$ var qStr = "SELECT VALUE c FROM SchoolEntities.Courses AS c "$ var c.5 = conn.CreateCo..an5*+$ c.5.Co..an5Te%t = qStr$ usin, *var r5r = c.5.E%ecuteRea5er*Co..an51ehavior.SequentialAccess++ 3 -hile *r5r.Rea5*++3 Console.riteLine*r5r.6etStrin,*2++$ 7 77 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. L&() to EntitiesEntity S)L with *b+ect Ser,icesEntity S)L withEntity C'ient/I;Q support IntelliSense Model dynamic 2ueries )eturn type 0&*ects 0&*ects or D&Data)ecordsD&Data)eader1er#ormance 2009 Gil Fink. All rights reserved. Choose Entity SQ/ withEntity Client. :ou wantto return streamed data :ou want to use EDM in existing applications Choose Entity SQ/ with 0&*ect Ser%ices. :ou want to express 2ueries that /I;Q doesn7t ena&le :ou want dynamic 2ueries :ou want the &est per#ormance 0therwise choose /I;Q to Entities 2009 Gil Fink. All rights reserved. Copyright SELA software & Education Labs Ltd. 14-18 aruch !irsch St.nei ra" #1$%$ &srae'www.se'a.co.i' 2009 Gil Fink. All rights reserved. E%ery entity in 0&*ectContext has a 0&*ectStateEntry 0&*ectContext uses 0&*ectStateEntries to track entity changes 2009 Gil Fink. All rights reserved. 1ersists &ack to the data storage all the changes made to entities,ses the 0&*ectStateEntries to &uild the rele%ant data storage commands,nit o# work patterncontext.SaveChanges()#

$%&& doesn't refresh the entities state after savecontext.SaveChanges(false)# 2009 Gil Fink. All rights reserved. Create entity in memory Create in memory manually,se one o# the entity7s generated create methodsvar de(artment = new )e(artment()#var course = Course.CreateCourse(*)# 2009 Gil Fink. All rights reserved. -dd entity =y -ssignment to an existing entity7s property=y adding to a EntityCollection using -dd method,se 0&*ectContext -ddo methodscourse.)e(artment = de(artment#de(artment.Courses.+dd(course)# context.+ddToCourses(course)# 2009 Gil Fink. All rights reserved. Change a property or re#erence to entity he changes will &e tracked &y the 0&*ectStateEntry - call to Sa%eChanges will create update command 2009 Gil Fink. All rights reserved. Entity must &e in hand in order to per#orm delete ,se the 0&*ectContext Delete0&*ect method&& course need to be at handcontext.)elete$b,ect(course)# 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Map procedures directly to 0&*ectContext as methods Map procedures to entities 0%erride automatically C,D &eha%iorSproc . Sproc . /unction/unctionSproc . Sproc . /unction/unction0ethod0ethod 0ethod0ethod 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Entity Framework IntroductionExploring the Entity Data ModelQuerying and Manipulating Entity Data ModelsCustomizing Entity Data Models ExamplesEF4Summary 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. he inheritance tree is create through one ta&le only 1> inheritance depends on conditional mapping he condition is used to de5ne records as di?erent types 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. -re elements o# the SSD/ -re created whene%er you map a %iew into the EDM -re a pro*ection o# data and there#ore are read only ,sing stored procedures you can add write #unctionality :ou can create your own De5ningQueries 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Entity Framework IntroductionExploring the Entity Data ModelQuerying and Manipulating Entity Data ModelsCustomizing Entity Data Models ExamplesEF4Summary 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Entity DataModelEntity DataModelDatabase&irst (*))Entity DataModelEntity DataModelModel&irst (*+) 2009 Gil Fink. All rights reserved. )) ),) DB Mapping -) Could get .cro'ded/0) 1o designer support on 2) )) &le3ibilit% from DB shape-) 1icel% .organi4ed/0) Designer supported256 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. Class Definition5calar #ropert% Definition1a*igation #ropert% Definition$*er%thing 2009 Gil Fink. All rights reserved. 2009 Gil Fink. All rights reserved. )) 7et #roduct-) 8ccessCategor%7et #roduct)) 7et #roduct-) 8ccessCategor%7et Categor%0) 7et Categor%7et #roduct7et Categor%$3plicit9mplicit 2009 Gil Fink. All rights reserved. 9n theor% thislooks reall% good9n theor% this alsolooks reall% goodThis is perfect: 2009 Gil Fink. All rights reserved. ;mm( 9"d rather be using !91