Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

ADO.

NET Entity Framework


ADO.NET Entity Framework abstracts the relational (logical) schema of the
data that is stored in a database and presents its conceptal schema to the
application. This abstraction eliminates the ob!ect"relational impedance mismatch
that is otherwise common in con#entional database"oriented programs. For
e$ample% in a con#entional database"oriented system% entries abot a cstomer and
their information can be stored in a &stomers table% their orders in an Orders table
and their contact information in yet another &ontacts table. The application that
deals with this database mst 'know' which information is in which table% i.e.% the
relational schema of the data is hard"coded into the application.
The disad#antage of this approach is that if this schema is changed the
application is not shielded from the change. Also% the application has to perform
()* !oins to tra#erse the relationships of the data elements in order to +nd related
data. For e$ample% to +nd the orders of a certain cstomer% the cstomer needs to
be selected from the &stomers table% the &stomers table needs to be !oined with
the Orders table% and the !oined tables need to be ,eried for the orders that are
linked to the cstomer. This model of tra#ersing relationships between items is #ery
di-erent from the model sed in ob!ect"oriented programming langages% where the
relationships of an ob!ect.s featres are e$posed as /roperties of the ob!ect and
accessing the property tra#erses the relationship. Also% sing ()* ,eries e$pressed
as strings% only to ha#e it processed by the database% keeps the programming
langage from making any garantees abot the operation and from pro#iding
compile time type information. The mapping of logical schema into the physical
schema that de+nes how the data is strctred and stored on the disk is the !ob of
the database system and client side data access mechanisms are shielded from it
as the database e$poses the data in the way speci+ed by its logical schema.
History0 #ersion 1 of Entity Framework (EF#1) was inclded with .NET Framework
2.3 (er#ice /ack 1 and 4isal (tdio 5667 (er#ice /ack 1% released on 11 Agst
5667. This #ersion has been widely critici8ed% e#en attracting a .#ote of no
con+dence. signed by se#eral hndred de#elopers. The second #ersion of Entity
Framework% named Entity Framework 9.6 (EF#9)% was released as part of .NET 9.6
on 15 April 5616 and has addressed many of the criticisms made of #ersion 1. A
third #ersion of Entity Framework% yet nnamed% is planned for release in +rst
,arter of 5611. There ha#e been se#eral betas (called &ommnity Technology
/re#iew) of it and the last &T/3 was released in December 5616.
Entity Data Model0 the Entity data model (ED:) speci+es the conceptal model
(&(D*) of the data #ia the Entity";elationship data model% which deals primarily with
Entities and the Associations they participate in. The ED: schema is e$pressed in
the (chema De+nition *angage ((D*)% which is an application of <:*. =n addition%
the mapping (:(*) of the elements of the conceptal schema (&(D*) to the storage
schema (((D*) mst also be speci+ed. The mapping speci+cation is also e$pressed
in <:*. 4isal (tdio also pro#ides Entity Designer% for #isal creation of the ED:
and the mapping speci+cation. The otpt of the tool is the <:* +le (>.edm$)
specifying the schema and the mapping. Edm$ +le contains EF metadata artifacts
(&(D*?:(*?((D* content). These 2 +les (csdl% msl% ssdl) can also be created or
edited by hand.
Developing an EDM Project cong!ring wit" Oracle#
Open a new pro!ect of type windows% name it as @OracleED:A% open @Add New =temA
window% select @ADO.NET Entity Data :odelA% name it as @(ample.edm$A and click
on Add Btton0
"After yo click on Add Btton it opens a wi8ard% select @Cenerate from databaseA
option in it D click Ne$t Btton0
"Now in the Ne$t Eindow click on New &onnection Btton0
"&lick on the &hange Btton0
(elect Oracle Database in the window opened and click OF0
"/ro#ide the connection details for Oracle% then click on Test &onnection btton to
check the connection details and then click on OF btton0
"Once yo click on Ok btton it opens the following window% in it select the radio
btton @Ges% inclde the sensiti#e data in the connection stringA and click on the
Ne$t Btton0
"Now it displays a window with the list of table of stored procedres select Emp and
Dept tables in it0
"&lick +nish btton which displays the list of tables we ha#e selected% relations
between the tables% also on the ;H( we will +nd a window @:odel BrowserA and in it
we can +nd or tables their colmns and also the constraint that are sed for
establishing relations between the tables0
"Here nder the tables we will be +nding some additional properties known as
@Na#igation /ropertiesA apart from the properties that will be generated for each
colmn of the table. Na#igation /roperties are sed for na#igating from one table to
the other as these tables ha#e relations between each other% sing those properties
we can retrie#e data from both these table withot sing any !oins.
Note0 Once the tables are selected and clicked on the +nish btton internally all the
re,ired classes representing the database with the name as Entities% selected
tables as well as properties representing all the colmns of tables selected as well
as methods which are re,ired to perform the database operations gets generated
same as we ha#e seen in case of $%N& to '&$. Go can check this by e$panding
the (ample.edm$ +le in soltion e$plorer and #iew the code nder
(ample.Designer.cs +le.
"Now /lace a DataCrid4iew control on the form and write the following code in its
form load e#ent0 Entities db I new Entities ()J (()reating o*ject o+ Entities class
+or esta*lis"ing connection
dataCrid4iew1.Data(orce I db.E:/s.(elect('it.Empno% it.Ename% it.Kob% it.(al%
it.Deptno')J
or
??dataCrid4iew1.Data(orce I
db.E:/s.(elect('it.Empno% it.Ename% it.Kob% it.(al%
it.Deptno').Ehere('it.DeptnoI26')J
or
??dataCrid4iew1.Data(orce I
db.E:/s.(elect('it.Empno% it.Ename% it.Kob% it.(al%
it.Deptno').Ehere('it.DeptnoI26').OrderBy('it.(al')J
or
dataCrid4iew1.Data(orce I
db.E:/s.(elect('it.Empno% it.Ename% it.Kob% it.(al%
it.Deptno').Ehere('it.DeptnoI26').OrderBy('it.(al Desc')J
Note0 @itA is an alias name representing the table and this is a standard alias name
that shold be sed for any table which cannot be changed. (elect% Ehere and
OrderBy are methods that prede+ned to access the data from re,ired tables.
Per+orming 'elect , DM$ Operations0 &reate 5 new forms as following0
=n the second Form change the modi+er as =nternal for all the 3 Te$tBo$Ls and =nsert
btton also so that they can be accessed from +rst form D in the +rst form change
the DataCrid4iew name as dg4iew.
)ode !nder First Form
Declarations0 Entities dbJ
-nder Form $oad0 db I new Entities()J
dg4iew.Data(orce I db.Emps.(elect('it.Empno% it.Ename% it.Kob% it.(al% it.Deptno')J
-nder %nsert .!tton0 Form2 f I new Form2()J f.btn(a#e.Te$t I '=nsert'J
f.(howDialog()J
dg4iew.Data(orce I db.Emps.(elect('it.Empno% it.Ename% it.Kob% it.(al% it.Deptno')J
-nder -pdate .!tton0 if (dg4iew.(elected;ows.&ont M 6) N
Form2 f I new Form2()J
f.te$tBo$1.Te$t I dg4iew.(elected;owsO6P.&ellsO6P.4ale.To(tring()J
f.te$tBo$5.Te$t I dg4iew.(elected;owsO6P.&ellsO1P.4ale.To(tring()J
f.te$tBo$2.Te$t I dg4iew.(elected;owsO6P.&ellsO5P.4ale.To(tring()J
f.te$tBo$9.Te$t I dg4iew.(elected;owsO6P.&ellsO2P.4ale.To(tring()J
f.te$tBo$3.Te$t I dg4iew.(elected;owsO6P.&ellsO9P.4ale.To(tring()J
f.btn(a#e.Te$t I 'Qpdate'J f.(howDialog()J
dg4iew.Data(orce I db.Emps.(elect('it.Empno% it.Ename% it.Kob% it.(al%
it.Deptno')J
R
else
:essageBo$.(how('(elect a record from Crid4iew to pdate'% 'Earning'%
:essageBo$Bttons.OF%
:essageBo$=con.Earning)J
-nder Delete .!tton0 if (dg4iew.(elected;ows.&ont M 6) N
if (:essageBo$.(how('Do yo wish to delete the recordS'% '&on+rmation'%
:essageBo$Bttons.GesNo%
:essageBo$=con.)estion) II Dialog;eslt.Ges) N
int empno I &on#ert.To=nt25(dg4iew.(elected;owsO6P.&ellsO6P.4ale)J
Emp ob! I db.Emps.(ingleOrDefalt(E IM E.Empno II empno)J
db.DeleteOb!ect(ob!)J
db.(a#e&hanges()J dg4iew.Data(orceIdb.Emps.(elect('it.Empno% it.Ename%
it.Kob% it.(al% it.Deptno')J
R
R
else
:essageBo$.(how('(elect a record from Crid4iew to delete'% 'Earning'%
:essageBo$Bttons.OF%
:essageBo$=con.Earning)J
)ode !nder 'econd Form
-nder %nsert .!tton0 Entities db I new Entities()J
if (btn(a#e.Te$t II '=nsert')
N
Emp ob! I new Emp()J
ob!.Empno I int./arse(te$tBo$1.Te$t)J
ob!.Ename I te$tBo$5.Te$tJ
ob!.Kob I te$tBo$2.Te$tJ
ob!.(al I decimal./arse(te$tBo$9.Te$t)J
ob!.Deptno I int./arse(te$tBo$3.Te$t)J
db.Emps.AddOb!ect(ob!)J
db.(a#e&hanges()J
R
else
N
int empno I int./arse(te$tBo$1.Te$t)J
Emp ob! I db.Emps.(ingleOrDefalt(E IM E.Empno II empno)J
ob!.Ename I te$tBo$5.Te$tJ
ob!.Kob I te$tBo$2.Te$tJ
ob!.(al I decimal./arse(te$tBo$9.Te$t)J
ob!.Deptno I int./arse(te$tBo$3.Te$t)J
db.(a#e&hanges()J
R
)alling 'tored Proced!res# we can call stored procedres also sing ED: !st
like in @*in, to (,lA% to call the (electTEmp stored procedre we ha#e de+ned nder
the :y/ackage earlier while discssing of ADO.Net% +rst open (ample.edm$ +le
right click on the +le and select @Qpdate :odel from DatabaseA0
"Now a window gets opened what we ha#e seen earlier as following0
"=n it e$pand the node (tored /rocedres and select the (/
@:G/A&FACE.(E*E&TTE:/A and click Finish btton which displays the procedre
nder :odel Browser nder the node (tored /rocedres.
"As we ha#e learnt in *in, to (,l that (tored /rocedres gets con#erted into method
here also the same thing happens bt or (tored /rocedre (electTEmp is retring
the data from the database in the form of a crsor as an Otpt parameter% so +rst
we need to con#ert it into a retrn type of or method which is being created% to do
this open the (oltion E$plorer right click on the (ample.edm$ +le and select the
option Open Eith.
=t opens the following window init select the option @<:* (Te$t) EditorA and click on
the Ok btton0
"Now it opens the (ample.edm$ +le in a <:* format in it yo will +nd a Tag 'c"ema
as following0
U(chema NamespaceI':odel.(tore' AliasI'(elf' /ro#iderI'De#art.Data.Oracle'
/ro#ider:anifestTokenI'O;A'
$mlns0storeI'http0??schemas.microsoft.com?ado?566V?15?edm?Entity(tore(chemaCenerator'
$mlnsI'http0??schemas.microsoft.com?ado?566W?65?edm?ssdl'M
"=n the (chema tag add the following content in the last line which shold look as
following0
U(chema NamespaceI':odel.(tore' AliasI'(elf' /ro#iderI'De#art.Data.Oracle'
/ro#ider:anifestTokenI'O;A'
$mlns0storeI'http0??schemas.microsoft.com?ado?566V?15?edm?Entity(tore(chemaCenerator'
$mlnsI'http0??schemas.microsoft.com?ado?566W?65?edm?ssdl'
$mlns0de#artI'http0??de#art.com?schemas?edml?(torage(chemaE$tensions?1.6'M
"Actally we are adding a new <:* Namespace in the abo#e case. Now go below the
+le there yo will +nd a tag F!nction as following0
UFnction NameI':G/A&FACET(E*E&TTE:/' AggregateI'false' Bilt=nI'false'
NiladicFnctionI'false' =s&omposableI'false'
/arameterType(emanticsI'Allow=mplicit&on#ersion'
(toreFnctionNameI':G/A&FACE.(E*E&TTE:/' (chemaI'(&OTT'M
U/arameter NameI'E:/&Q;' TypeI';EF &Q;(O;' :odeI'Ot' ?M
U?FnctionM
"Qnder the Fnction Tag we will +nd a (b Tag Parameter which is or Otpt
parameter Emp&r what we de+ned as /e+ )!rsor which shold be changed as a
retrn type of the method% do the following to change% so that after modi+cation it
shold look as below0
UFnction NameI':G/A&FACET(E*E&TTE:/' AggregateI'false' Bilt=nI'false'
NiladicFnctionI'false' =s&omposableI'false'
/arameterType(emanticsI'Allow=mplicit&on#ersion'
(toreFnctionNameI':G/A&FACE.(E*E&TTE:/' (chemaI'(&OTT'
de#art0;eslt(et/arameterNameI'E:/&Q;'M
UX"" U/arameter NameI'E:/&Q;' TypeI';EF &Q;(O;' :odeI'Ot' ?M ""M
U?FnctionM
"Now open the :odel Browser% e$pand the node (tored /rocedres right click on the
stored procedre @:G/A&FACE.(E*E&TTE:/A and select the option @Add Fnction
=mportA as following0
1
"Now Opens the following window in it select the below options and
%nvoking t"e a*ove Proced!re0 Once yo click on ok btton it generates a new
method @:G/A&FACET(E*E&TTE:/A nder the class @EntitiesA% check the
Designer.cs +le for it. Add a new Eindows Form in the pro!ect% place a DataCrid4iew
on it and change the name of it as dg4iew and write the following code0
-nder Form $oad0
Entities db I new Entities()J
dg4iew.Data(orce I db.:G/A&FACET(E*E&TTE:/()J
/etrieving data +rom single or m!ltiple ta*les0 we can retrie#e data from 1 or
more table(s) also at a time sing @*in, to EntitiesA ,ery langage which will be
same as we ha#e sed in case of ,erying data in *in,% with some minor changes.
Take a new windows form and design it as following% change the DataCrid4iew name
as dg4iew and write the code0
Declarations0 Entities dbJ
-nder Form $oad0
db I new Entities()J comboBo$1.Data(orce I db.E:/s.(elect('it.Kob').Distinct()J
comboBo$1.Display:ember I 'Kob'J
dg4iew.Data(orce I from E in db.E:/s select new N E.E:/NO% E.ENA:E% E.KOB%
E.:C;% E.H=;EDATE% E.(A*% E.&O::% E.DE/TNO RJ
-nder )om*o.o0 'elected%nde0)"anged0
dg4iew.Data(orce I from E in db.E:/s where E.KOB II comboBo$1.Te$t select
new N E.E:/NO% E.ENA:E% E.KOB% E.:C;% E.H=;EDATE% E.(A*% E.&O::% E.DE/TNO RJ
-nder Emp )o!nt 1ro!p .y Deptno .!tton0
dg4iew.Data(orce I from E in db.E:/s grop E by E.DE/TNO into C orderby C.Fey
select new N Deptno I C.Fey% Emp&ont I C.&ont() RJ
-nder Emp )o!nt 1ro!p .y Deptno wit" Having )la!se .!tton0
dg4iew.Data(orce I from E in db.E:/s grop E by E.DE/TNO into C where
C.&ont() M 2 orderby C.Fey select new N Deptno I C.Fey% Emp&ont I
C.&ont() RJ
-nder Emp )o!nt 1ro!p .y 2o* .!tton0
dg4iew.Data(orce I from E in db.E:/s grop E by E.KOB into C orderby C.Fey
select new N Kob I C.Fey% Kob&ont I C.&ont() RJ
-nder Ma0 'al 1ro!p .y Deptno .!tton0
dg4iew.Data(orce I from E in db.E:/s grop E by E.DE/TNO into C orderby C.Fey
select new N Deptno I C.Fey% :a$(al I C.:a$(i IM i.(A*) RJ
-nder Min 'al 1ro!p .y 2o* .!tton#
dg4iew.Data(orce I from E in db.E:/s grop E by E.KOB into C orderby C.Fey
select new N Kob I C.Fey% :in(al I C.:in(i IM i.(A*) RJ
-nder Data +rom M!ltiple Ta*les .!tton0
dg4iew.Data(orce I from E in db.E:/s select new N E.E:/NO% E.ENA:E% E.KOB%
E.:C;% E.H=;EDATE% E.(A*% E.&O::% E.DE/T.DE/TNO% E.DE/T.DNA:E% E.DE/T.*O& RJ
-nder 1et 'elected )ol!mns .!tton0
dg4iew.Data(orce I from E in db.E:/s select new N E.E:/NO% E.ENA:E% E.KOB%
E.(A*% E.DE/TNO RJ
Note0 To retrie#e data from 5 tables with relations we donLt re,ire writing any !oins%
directly by sing the na#igations properties nder the tables we can retrie#e data
from both tables. There is no Ha#ing &lase in *=N)% Ehere clase only can be sed
as Ha#ing &ase also% if we se it after Crop By &lase Ehere &lase works like
Ha#ing &lase.
$%N& 3s EDM#
" *=N) to ()* was de#eloped for rapid application de#elopment (;AD)% whereas
Entity Framework was de#eloped for enterprise application de#elopment.
" *=N) to ()* works with the ob!ects in a database whereas Entity Framework
works with the conceptal model of a database. As e$plained earlier% these
are two di-erent things which frther mean that the Entity Framework allows
yo to perform ,eries against relationships between entities% mapping single
entities to mltiple tables% and so on.
" *=N) to ()* has the ability to call (tored /rocedres% bt not map them with
the reslts where as Entity Framework is all abot mapping reslts to entities.

You might also like