You are on page 1of 7

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.
Open a new pro!ect of type windows name it as ED:% open the add new item
window% select @ADO.NET Entity Data :odelA% name it as @(ample.edm$A% &lick Add
btton% which opens a wi8ard select @Benerate from databaseA% click Ne$t btton%
&hoose :icrosoft ()* (er#er database and click on @OkA btton% pro#ide the
connection details% choose yor @&(harpDCA database% click on @OkA btton% select
@Des% inclde the sensiti#e data in the connection stringA% click on @Ne$tA btton%
e$pand Tables node% choose the tables Emp E Dept% click Finish which displays the
tables that are selected on a new window.
Note0 Once the tables are selected and clicked on the +nish btton internally all the
re,ired classes representing the database% 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. Do can check this by
e$panding the .edm$ +le in soltion e$plorer and #iew it in the Designer.cs +le.
/lace a DataBrid4iew control on the form and write the following code in its form
load e#ent0
&(harpDCEntities db F new &(harpDCEntities ()G
dataBrid4iew1.Data(orce F db.Emps.(elect('it.Empno% it.Ename% it.Hob% it.(al%
it.Deptno')G
Or
dataBrid4iew1.Data(orce F
db.Emps.(elect('it.Empno% it.Ename% it.Hob% it.(al%
it.Deptno' ).Ihere('it.DeptnoF26')G
or
dataBrid4iew1.Data(orce F
db.Emps.(elect('it.Empno% it.Ename% it.Hob% it.(al%
it.Deptno').OrderCy('it.Deptno')G
Note0 &(harpDCEntities is the class which is generated that represents yor
database &(harp.
Performing Select & DM O!erations0 &reate 5 forms as following
=n the second Form change the modi+er as =nternal for all the 3 Te$tCo$Js and =nsert
btton also so that they can be accessed from +rst form E in the +rst form change
the DataBrid4iew name as dg4iew.
"ode #nder First Form
Declarations0 &(harpDCEntities dbG
$nder Form oad0 db F new &(harpDCEntities()G
dg4iew.Data(orce F db.Emps.(elect('it.Empno% it.Ename% it.Hob% it.(al% it.Deptno')G
$nder %nsert &#tton0 Form2 f F new Form2()G f.btn(a#e.Te$t F '=nsert'G
f.(howDialog()G
dg4iew.Data(orce F db.Emps.(elect('it.Empno% it.Ename% it.Hob% it.(al% it.Deptno')G
$nder $!date &#tton0 if (dg4iew.(elected;ows.&ont K 6) L
Form2 f F new Form2()G
f.te$tCo$1.Te$t F dg4iew.(elected;owsM6N.&ellsM6N.4ale.To(tring()G
f.te$tCo$5.Te$t F dg4iew.(elected;owsM6N.&ellsM1N.4ale.To(tring()G
f.te$tCo$2.Te$t F dg4iew.(elected;owsM6N.&ellsM5N.4ale.To(tring()G
f.te$tCo$9.Te$t F dg4iew.(elected;owsM6N.&ellsM2N.4ale.To(tring()G
f.te$tCo$3.Te$t F dg4iew.(elected;owsM6N.&ellsM9N.4ale.To(tring()G
f.btn(a#e.Te$t F 'Opdate'G f.(howDialog()G
dg4iew.Data(orce F db.Emps.(elect('it.Empno% it.Ename% it.Hob% it.(al%
it.Deptno')G
P
else
:essageCo$.(how('(elect a record from Brid4iew to pdate'% 'Iarning'%
:essageCo$Cttons.OQ%
:essageCo$=con.Iarning)G
$nder Delete &#tton0 if (dg4iew.(elected;ows.&ont K 6) L
if (:essageCo$.(how('Do yo wish to delete the recordR'% '&on+rmation'%
:essageCo$Cttons.DesNo%
:essageCo$=con.)estion) FF Dialog;eslt.Des) L
int empno F &on#ert.To=nt25(dg4iew.(elected;owsM6N.&ellsM6N.4ale)G
Emp ob! F db.Emps.(ingleOrDefalt(E FK E.Empno FF empno)G
db.DeleteOb!ect(ob!)G
db.(a#e&hanges()G dg4iew.Data(orceFdb.Emps.(elect('it.Empno% it.Ename%
it.Hob% it.(al% it.Deptno')G
P
P
else
:essageCo$.(how('(elect a record from Brid4iew to delete'% 'Iarning'%
:essageCo$Cttons.OQ%
:essageCo$=con.Iarning)G
"ode #nder Second Form
$nder %nsert &#tton0 &(harpDCEntities db F new &(harpDCEntities()G
if (btn(a#e.Te$t FF '=nsert') L
Emp ob! F new Emp()G ob!.Empno F int./arse(te$tCo$1.Te$t)G ob!.Ename F
te$tCo$5.Te$tG
ob!.Hob F te$tCo$2.Te$tG ob!.(al F decimal./arse(te$tCo$9.Te$t)G ob!.Deptno F
int./arse(te$tCo$3.Te$t)G
db.Emps.AddOb!ect(ob!)G db.(a#e&hanges()G
P
else L
int empno F int./arse(te$tCo$1.Te$t)G Emp ob! F db.Emps.(ingleOrDefalt(E FK
E.Empno FF empno)G
ob!.Ename F te$tCo$5.Te$tG ob!.Hob F te$tCo$2.Te$tG ob!.(al F
decimal./arse(te$tCo$9.Te$t)G
ob!.Deptno F int./arse(te$tCo$3.Te$t)G db.(a#e&hanges()G P
"alling Stored Proced#res' we can call stored procedres also sing ED:% to call
it +rst de+ne a (/ in s,l ser#er database as following0
"reate Proced#re Select(Em!)*De!tno %nt + N#ll,
As
&egin
%f *De!tno %s N#ll
Select Em!no- Ename- .o/- Mgr- Hiredate- Sal- "omm- De!tno From Em!
Else
Select Em!no- Ename- .o/- Mgr- Hiredate- Sal- "omm- De!tno From Em!
01ere De!tno + *De!tno
End
%n2oking t1e a/o2e Proced#re0
To in#oke the abo#e (/ go to (ample.edm$ +le and in the ;S( we +nd a
window @:odel CrowserA% open it% right click on it and select @Opdate :odel from
DatabaseA which opens a @Opdate Ii8ardA% in it e$pand the node @(tored
/rocedresA% select the (/ @(electTEmpA we ha#e de+ned abo#e E click Finish%
which adds the (/ nder (tored /rocedres node of :odel Crowser% right click on
the (/ @(electTEmpA% select @Add Fnction =mportA which opens a window% choose
the ;adio Ctton @EntitiesA% select @EmpA from the &ombo Co$ and click on @OkA
btton.
Note0 once yo click on the ok btton it generates a new method @(electTEmpA
nder the class @&(harpDCEntitiesA% check the Designer.cs +le for it.
&reate a new Form as following% change the name of DataBrid4iew as dg4iew and
write the code0
Declarations0 &(harpDCEntities dbG bool Uag F falseG
$nder Form oad0
db F new &(harpDCEntities()G comboCo$1.Data(orce F
db.Emps.(elect('it.Deptno').Distinct()G
comboCo$1.Display:ember F 'Deptno'G dg4iew.Data(orce F db.(electTEmp(nll)G
Uag F treG
$nder "om/o&o3 Selected%nde3"1anged0 if (Uag) L
int deptno F int./arse(comboCo$1.Te$t)G dg4iew.Data(orce F
db.(electTEmp(deptno)G
P
4etrie2ing data from single or m#lti!le 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 and write the code0
Declarations0 &(harpDCEntities dbG
$nder Form oad0
db F new &(harpDCEntities()G
comboCo$1.Data(orce F db.Emps.(elect('it.Hob').Distinct()G
comboCo$1.Display:ember F 'Hob'G
dataBrid4iew1.Data(orce F from E in db.Emps select new L E.Empno% E.Ename%
E.Hob% E.:gr% E.SireDate% E.(al% E.&omm% E.Deptno PG
$nder "om/o&o3 Selected%nde3"1anged0
dataBrid4iew1.Data(orce F from E in db.Emps where E.Hob FF comboCo$1.Te$t
select new L E.Empno% E.Ename% E.Hob% E.:gr% E.SireDate% E.(al% E.&omm%
E.Deptno PG
$nder 5ro#! &y "la#se &#tton60
dataBrid4iew1.Data(orce F from E in db.Emps grop E by E.Deptno into B select
new L Deptno F B.Qey% Emp&ont F B.&ont() PG
$nder 5ro#! &y "la#se &#tton70
dataBrid4iew1.Data(orce F from E in db.Emps grop E by E.Deptno into B select
new L Deptno F B.Qey% :a$(al F B.:a$(i FK i.(al) PG
$nder 5ro#! &y "la#se &#tton80
dataBrid4iew1.Data(orce F from E in db.Emps grop E by E.Hob into B select new
L Hob F B.Qey% Hob&ont F B.&ont() PG
$nder 5ro#! &y "la#se &#tton90
dataBrid4iew1.Data(orce F from E in db.Emps grop E by E.Hob into B select new
L Hob F B.Qey% :in(al F B.:in(i FK i.(al) PG
$nder Order &y "la#se &#tton0
dataBrid4iew1.Data(orce F from E in db.Emps orderby E.(al select new
L E.Empno% E.Ename% E.Hob% E.:gr% E.SireDate% E.(al% E.&omm% E.Deptno PG
$nder M#lti!le Ta/les &#tton0
dataBrid4iew1.Data(orce F from E in db.Emps select new L E.Empno% E.Ename%
E.Hob% E.:gr% E.SireDate% E.(al% E.&omm% E.Dept.Deptno% E.Dept.DName%
E.Dept.*oc PG
%N: ;s 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