8/11/2019 EDM With Oracle
1/15
ADO.NET Entity FrameworkADO.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 andtheir 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 arelinked 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!ects 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 withEntities and the Associations they participate in. The ED: schema is e$pressed in
the chema De+nition *angage (D*)% which is an application of
8/11/2019 EDM With Oracle
2/15
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:% open @Add New =tem
window% select @ADO.NET Entity Data :odel% name it as @ample.edm$ and click
on Add Btton0
"After yo click on Add Btton it opens a wi8ard% select @Cenerate from database
option in it click Ne$t Btton0
8/11/2019 EDM With Oracle
3/15
"Now in the Ne$t indow click on New &onnection Btton0
"&lick on the &hange Btton0
8/11/2019 EDM With Oracle
4/15
elect Oracle Database in the window opened and click O0
"/ro#ide the connection details for Oracle% then click on Test &onnection btton to
check the connection details and then click on O btton0
8/11/2019 EDM With Oracle
5/15
"Once yo click on Ok btton it opens the following window% in it select the radiobtton @Ges% inclde the sensiti#e data in the connection string and click on the
Ne$t Btton0
8/11/2019 EDM With Oracle
6/15
"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 Browser and in it
8/11/2019 EDM With Oracle
7/15
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 /roperties 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% selectedtables 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.Dataorce I db.E:/s.elect('it.Empno% it.Ename% it.Kob% it.al%
it.Deptno')J
or
??dataCrid4iew1.Dataorce I
db.E:/s.elect('it.Empno% it.Ename% it.Kob% it.al%
it.Deptno').here('it.DeptnoI26')J
or
??dataCrid4iew1.Dataorce I
8/11/2019 EDM With Oracle
8/15
db.E:/s.elect('it.Empno% it.Ename% it.Kob% it.al%
it.Deptno').here('it.DeptnoI26').OrderBy('it.al')J
or
dataCrid4iew1.Dataorce I
db.E:/s.elect('it.Empno% it.Ename% it.Kob% it.al%
it.Deptno').here('it.DeptnoI26').OrderBy('it.al Desc')JNote0 @it 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% here 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 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.Dataorce 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.btna#e.Te$t I '=nsert'J
f.howDialog()J
dg4iew.Dataorce 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)
Form2 f I new Form2()J
f.te$tBo$1.Te$t I dg4iew.elected;ows6P.&ells6P.4ale.Totring()J
f.te$tBo$5.Te$t I dg4iew.elected;ows6P.&ells1P.4ale.Totring()J
f.te$tBo$2.Te$t I dg4iew.elected;ows6P.&ells5P.4ale.Totring()J
f.te$tBo$9.Te$t I dg4iew.elected;ows6P.&ells2P.4ale.Totring()J
f.te$tBo$3.Te$t I dg4iew.elected;ows6P.&ells9P.4ale.Totring()J
f.btna#e.Te$t I 'Qpdate'J f.howDialog()J
dg4iew.Dataorce I db.Emps.elect('it.Empno% it.Ename% it.Kob% it.al%
it.Deptno')J
R
else
8/11/2019 EDM With Oracle
9/15
:essageBo$.how('elect a record from Crid4iew to pdate'% 'arning'%
:essageBo$Bttons.O%
:essageBo$=con.arning)J
-nder Delete !tton0 if (dg4iew.elected;ows.&ont M 6)
if (:essageBo$.how('Do yo wish to delete the recordS'% '&on+rmation'%
:essageBo$Bttons.GesNo%:essageBo$=con.estion) II Dialog;eslt.Ges)
int empno I &on#ert.To=nt25(dg4iew.elected;ows6P.&ells6P.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.DataorceIdb.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'% 'arning'%:essageBo$Bttons.O%
:essageBo$=con.arning)J
)ode !nder 'econd Form
-nder %nsert !tton0 Entities db I new Entities()J
if (btna#e.Te$t II '=nsert')
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
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$tJob!.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 ,l% to call the electEmp stored procedre we ha#e de+ned nder
8/11/2019 EDM With Oracle
10/15
the :y/ackage earlier while discssing of ADO.Net% +rst open ample.edm$ +le
right click on the +le and select @Qpdate :odel from Database0
"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&ACE.E*E&TE:/ and click Finish btton which displays the procedre
nder :odel Browser nder the node tored /rocedres.
8/11/2019 EDM With Oracle
11/15
"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 electEmp 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 ith.
=t opens the following window init select the option @
8/11/2019 EDM With Oracle
12/15
"=n the chema tag add the following content in the last line which shold look asfollowing0
UchemaNamespaceI':odel.tore'AliasI'elf'/ro#iderI'De#art.Data.Oracle'/ro#ider:anifestTokenI'O;A'$mlns0storeI'http0??schemas.microsoft.com?ado?566V?15?edm?EntitytorechemaCenerator'$mlnsI'http0??schemas.microsoft.com?ado?566W?65?edm?ssdl'$mlns0de#artI'http0??de#art.com?schemas?edml?toragechemaE$tensions?1.6'M
"Actally we are adding a new
8/11/2019 EDM With Oracle
13/15
1
"Now Opens the following window in it select the below options and
8/11/2019 EDM With Oracle
14/15
%nvoking t"e a*ove Proced!re0 Once yo click on ok btton it generates a new
method @:G/A&ACEE*E&TE:/ nder the class @Entities% check the
Designer.cs +le for it. Add a new indows 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 $oad0Entities db I new Entities()J
dg4iew.Dataorce I db.:G/A&ACEE*E&TE:/()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 Entities ,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.Dataorce I db.E:/s.elect('it.Kob').Distinct()J
comboBo$1.Display:ember I 'Kob'J
dg4iew.Dataorce I from E in db.E:/s select new E.E:/NO% E.ENA:E% E.KOB%
E.:C;% E.H=;EDATE% E.A*% E.&O::% E.DE/TNO RJ
-nder )om*oo0 'elected%nde0)"anged0
dg4iew.Dataorce I from E in db.E:/s where E.KOB II comboBo$1.Te$t select
new 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.Dataorce I from E in db.E:/s grop E by E.DE/TNO into C orderby C.ey
select new Deptno I C.ey% Emp&ont I C.&ont() RJ
-nder Emp )o!nt 1ro!p y Deptno wit" Having )la!se !tton0
dg4iew.Dataorce I from E in db.E:/s grop E by E.DE/TNO into C where
C.&ont() M 2 orderby C.ey select new Deptno I C.ey% Emp&ont I
C.&ont() RJ
8/11/2019 EDM With Oracle
15/15
-nder Emp )o!nt 1ro!p y 2o* !tton0
dg4iew.Dataorce I from E in db.E:/s grop E by E.KOB into C orderby C.ey
select new Kob I C.ey% Kob&ont I C.&ont() RJ
-nder Ma0 'al 1ro!p y Deptno !tton0
dg4iew.Dataorce I from E in db.E:/s grop E by E.DE/TNO into C orderby C.ey
select new Deptno I C.ey% :a$al I C.:a$(i IM i.A*) RJ-nder Min 'al 1ro!p y 2o* !tton#
dg4iew.Dataorce I from E in db.E:/s grop E by E.KOB into C orderby C.ey
select new Kob I C.ey% :inal I C.:in(i IM i.A*) RJ
-nder Data +rom M!ltiple Ta*les !tton0
dg4iew.Dataorce I from E in db.E:/s select new 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.Dataorce I from E in db.E:/s select new 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% here clase only can be sed
as Ha#ing &ase also% if we se it after Crop By &lase here &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% theseare 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.