getting started with entity framework 4

Upload: adv-sunil-joshi

Post on 06-Jul-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 Getting Started With Entity Framework 4

    1/21

    1 | P a g e 0

    Getting Started with Entity Framework4.0 Database First and ASP.NET 4 Web

    FormsBy Tom DykstraDe!ember "# $0%0Print

     The &ontoso 'ni(ersity sam)*e web a))*i!ation demonstrates how to !reate ASP.NET Web

    Forms a))*i!ations +sing the Entity Framework 4.0 and ,is+a* St+dio $0%0. The sam)*e

    a))*i!ation is a website -or a !tiona* &ontoso 'ni(ersity. /t in!*+des -+n!tiona*ity s+!h as

    st+dent admission# !o+rse !reation# and instr+!tor assignments.

     The t+toria* shows eam)*es in &1. The down*oadab*e sam)*e !ontains !ode in both &1 and

    ,is+a* Basi!.

    Database First There are three ways yo+ !an work with data in the Entity Framework2 DatabaseFirst # Model First # and Code First . This t+toria* is -or Database First. For in-ormationabo+t the di3eren!es between these workows and g+idan!e on how to !hoose the best one

    -or yo+r s!enario# see Entity Framework De(e*o)ment Workows.

    Web Forms

     This t+toria* series +ses the ASP.NET Web Forms mode* and ass+mes yo+ know how to work

    with ASP.NET Web Forms in ,is+a* St+dio. /- yo+ don5t# see Getting Started with ASP.NET 4.6

    Web Forms. /- yo+ )re-er to work with the ASP.NET 7,& -ramework# see Getting Started with

    the Entity Framework +sing ASP.NET 7,&.

    So-tware (ersions

    Shown in the

    tutorial Also works with

    Windows 7 Windows 8

    Visual Studio

    2010

    Visual Studio 2010 Express for Web.

    The tutorial has not been tested with later versions of Visual Studio. There are man

    differen!es in menu sele!tions" dialo# boxes" and templates.

    .$ET %

    .$ET %.& is ba!'ward !ompatible with .$ET %" but the tutorial has not been tested with

    .$ET %.&.

    Entit

    (ramewor' %

    The tutorial has not been tested with later versions of Entit (ramewor'.

    http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-1#author-36250http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-1#author-36250http://code.msdn.microsoft.com/ASPNET-Web-Forms-97f8ee9ahttp://msdn.microsoft.com/en-us/library/ms178359.aspx#dbfmfcfhttp://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/introduction-and-overviewhttp://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/introduction-and-overviewhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-applicationhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-applicationhttp://code.msdn.microsoft.com/ASPNET-Web-Forms-97f8ee9ahttp://msdn.microsoft.com/en-us/library/ms178359.aspx#dbfmfcfhttp://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/introduction-and-overviewhttp://www.asp.net/web-forms/tutorials/aspnet-45/getting-started-with-aspnet-45-web-forms/introduction-and-overviewhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-applicationhttp://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-applicationhttp://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-1#author-36250

  • 8/16/2019 Getting Started With Entity Framework 4

    2/21

    2 | P a g e 0

    Startin# with Entit (ramewor' &" E( uses b default the Db&ontet AP/ that wasintrodu!ed with E( %.1. The Entit)ataSour!e !ontrol was desi#ned to use

    the8b9e!t&ontet *+,. (or information about how to use the Entit)ataSour!e !ontrolwith the Db&ontet *+," see this blo# post.

    :+estions/- yo+ ha(e ;+estions that are not dire!t*y re*ated to the t+toria*# yo+ !an )ost them to

    the ASP.NET Entity Framework -or+m# the Entity Framework and

  • 8/16/2019 Getting Started With Entity Framework 4

    3/21

    3 | P a g e 0

    &reating the Web A))*i!ation To start the t+toria*# o)en ,is+a* St+dio and then !reate a new ASP.NET Web A))*i!ation

    Pro9e!t +sing theASP.NET Web Application tem)*ate2

  • 8/16/2019 Getting Started With Entity Framework 4

    4/21

    4 | P a g e 0

     This tem)*ate !reates a web a))*i!ation )ro9e!t that a*ready in!*+des a sty*e sheet and

    master )ages2

  • 8/16/2019 Getting Started With Entity Framework 4

    5/21

    5 | P a g e 0

    8)en the Site.Master  *e and !hange >7y ASP.NET A))*i!ation> to ?&ontoso 'ni(ersity@.

    h%

      &ontoso 'ni(ersity

    Ch%

    Find the Menu !ontro* named Na(igation7en+ and re)*a!e it with the -o**owing mark+)#whi!h adds men+ items -or the )ages yo+=** be !reating.

      as)27en+ /D>Na(igation7en+> r+nat>ser(er> &ss&*ass>men+> 

    Enab*e,iewState>-a*se>

      /n!*+deSty*eB*o!k>-a*se> 8rientation>orionta*>

      /tems

      as)27en+/tem Na(igate'r*>CDe-a+*t.as)>  Tet>ome> C

      as)27en+/tem Na(igate'r*>CAbo+t.as)>  Tet>Abo+t> C  as)27en+/tem Na(igate'r*>CSt+dents.as)>  Tet>St+dents>

      as)27en+/tem Na(igate'r*>CSt+dentsAdd.as)>  Tet>Add St+dents>

    C

      Cas)27en+/tem

      as)27en+/tem Na(igate'r*>C&o+rses.as)>  Tet>&o+rses>

      as)27en+/tem Na(igate'r*>C&o+rsesAdd.as)>  Tet>Add &o+rses> C

      Cas)27en+/tem

  • 8/16/2019 Getting Started With Entity Framework 4

    6/21

    6 | P a g e 0

      as)27en+/tem Na(igate'r*>C/nstr+!tors.as)>  Tet>/nstr+!tors>

      as)27en+/tem Na(igate'r*>C/nstr+!tors&o+rses.as)>  Tet>&o+rse

    Assignments> C

      as)27en+/tem Na(igate'r*>C8H!eAssignments.as)>  Tet>8H!e

    Assignments> C

      Cas)27en+/tem  as)27en+/tem Na(igate'r*>CDe)artments.as)>  Tet>De)artments>

      as)27en+/tem Na(igate'r*>CDe)artmentsAdd.as)>  Tet>Add

    De)artments> C

      Cas)27en+/tem

      C/tems

      Cas)27en+

    8)en the Default.aspx  )age and !hange the &ontent !ontro* named Body&ontent to this2

    as)2&ontent /D>Body&ontent> r+nat>ser(er> &ontentP*a!eo*der/D>7ain&ontent>

      h$

      We*!ome to &ontoso 'ni(ersityI

      Ch$

    Cas)2&ontent

     Jo+ now ha(e a sim)*e home )age with *inks to the (ario+s )ages that yo+=** be !reating2

    &reating the DatabaseFor these t+toria*s# yo+=** +se the Entity Framework data mode* designer to a+tomati!a**y

    !reate the data mode* based on an eisting database Ko-ten !a**ed the database-rst  a))roa!hL. An a*ternati(e that=s not !o(ered in this t+toria* series is to !reate the datamode* man+a**y and then ha(e the designer generate s!ri)ts that !reate the database

    Kthe model-rst  a))roa!hL.For the databaseMrst method +sed in this t+toria*# the net ste) is to add a database to the

    site. The easiest way is to rst down*oad the )ro9e!t that goes with this t+toria*. Then rightM

    !*i!k the App_Data -o*der# se*e!t Add Existing Ite# and se*e!tthe School.mdf  database *e -rom the down*oaded )ro9e!t.An a*ternati(e is to -o**ow the instr+!tions at &reating the S!hoo* Sam)*e Database. Whether

    yo+ down*oad the database or !reate it# !o)y the School.mdf  *e -rom the -o**owing -o*derto yo+r a))*i!ation=s App_Data -o*der2PO8GOA7F/

  • 8/16/2019 Getting Started With Entity Framework 4

    7/21

    ! | P a g e 0

    /- yo+ !reate the database -rom a s!ri)t# )er-orm the -o**owing ste)s to !reate a database

    diagram2

    %. /n Se"#e" Explo"e"# e)and $ata %onnections# e)and School.mdf # rightM!*i!k $atabase $iag"as# and se*e!tAdd Ne& $iag"a.

    $. Se*e!t a** o- the tab*es and then !*i!k Add.

    S:< Ser(er !reates a database diagram that shows tab*es# !o*+mns in the tab*es# and

    re*ationshi)s between the tab*es. Jo+ !an mo(e the tab*es aro+nd to organie them

    howe(er yo+ *ike.

    ". Sa(e the diagram as >S!hoo*Diagram> and !*ose it.

  • 8/16/2019 Getting Started With Entity Framework 4

    8/21

    ' | P a g e 0

    /- yo+ down*oad the School.mdf  *e that goes with this t+toria*# yo+ !an (iew the databasediagram by do+b*eM!*i!king Sc(ool$iag"a +nder $atabase $iag"as in Se"#e"

    Explo"e".

     The diagram *ooks something *ike this Kthe tab*es might be in di3erent *o!ations -rom what=s

    shown hereL2

  • 8/16/2019 Getting Started With Entity Framework 4

    9/21

    ) | P a g e 0

    &reating the Entity Framework Data 7ode*Now yo+ !an !reate an Entity Framework data mode* -rom this database. Jo+ !o+*d !reate

    the data mode* in the root -o*der o- the a))*i!ation# b+t -or this t+toria* yo+=** )*a!e it in a

    -o*der named DAL K-or Data A!!ess

  • 8/16/2019 Getting Started With Entity Framework 4

    10/21

    1 | P a g e 0

     This starts the Entity Data 7ode* Wiard. /n the rst wiard ste)# the /ene"ate 0"o

    database o)tion is se*e!ted by de-a+*t. &*i!k Next.

    /n the %(oose o*" $ata %onnection ste)# *ea(e the de-a+*t (a*+es and !*i!k Next. The

    S!hoo* database is se*e!ted by de-a+*t and the !onne!tion setting is sa(ed in

    the Web.con *e as Sc(oolEntities.

  • 8/16/2019 Getting Started With Entity Framework 4

    11/21

    11 | P a g e 0

    /n the %(oose o*" $atabase +bects wiard ste)# se*e!t a** o- the tab*es

    e!e)t sysdiagrams Kwhi!h was !reated -or the diagram yo+ generated ear*ierL and then

    !*i!k inis(.

  • 8/16/2019 Getting Started With Entity Framework 4

    12/21

    12 | P a g e 0

    A-ter it=s nished !reating the mode*# ,is+a* St+dio shows yo+ a gra)hi!a* re)resentation o-

    the Entity Framework ob9e!ts KentitiesL that !orres)ond to yo+r database tab*es. KAs with the

    database diagram# the *o!ation o- indi(id+a* e*ements might be di3erent -rom what yo+ see

    in this i**+stration. Jo+ !an drag the e*ements aro+nd to mat!h the i**+stration i- yo+ want.L

  • 8/16/2019 Getting Started With Entity Framework 4

    13/21

    13 | P a g e 0

    E)*oring the Entity Framework Data 7ode*

  • 8/16/2019 Getting Started With Entity Framework 4

    14/21

    14 | P a g e 0

     Jo+ !an see that the entity diagram *ooks (ery simi*ar to the database diagram# with a

    !o+)*e o- di3eren!es. 8ne di3eren!e is the addition o- symbo*s at the end o- ea!h

    asso!iation that indi!ate the ty)e o- asso!iation Ktab*e re*ationshi)s are !a**ed entity

    asso!iations in the data mode*L2

    A oneMtoMeroMorMone asso!iation is re)resented by >%> and >0..%>.

    /n this !ase# a Person entity may or may not be asso!iated with

    an 8H!eAssignment entity. An8H!eAssignment entity m+st be asso!iated with

    a Person entity. /n other words# an instr+!tor may or may not be assigned to an oH!e#

    and any oH!e !an be assigned to on*y one instr+!tor.

    • A oneMtoMmany asso!iation is re)resented by >%> and >>.

  • 8/16/2019 Getting Started With Entity Framework 4

    15/21

    15 | P a g e 0

    /n this !ase# a Person entity may or may not ha(e asso!iated St+dentGrade entities.

    A St+dentGrade entity m+st be asso!iated with one Person entity. St+dentGrade entities

    a!t+a**y re)resent enro**ed !o+rses in this database i- a st+dent is enro**ed in a !o+rse

    and there=s no grade yet# the Grade )ro)erty is n+**. /n other words# a st+dent may not

    be enro**ed in any !o+rses yet# may be enro**ed in one !o+rse# or may be enro**ed in

    m+*ti)*e !o+rses. Ea!h grade in an enro**ed !o+rse a))*ies to on*y one st+dent.

    • A manyMtoMmany asso!iation is re)resented by >> and >>.

  • 8/16/2019 Getting Started With Entity Framework 4

    16/21

    16 | P a g e 0

    /n this !ase# a Person entity may or may not ha(e asso!iated &o+rse entities# and there(erse is a*so tr+e2 a&o+rse entity may or may not ha(e asso!iated Person entities. /n

    other words# an instr+!tor may tea!h m+*ti)*e !o+rses# and a !o+rse may be ta+ght by

    m+*ti)*e instr+!tors. K/n this database# this re*ationshi) a))*ies on*y to instr+!tors it

    does not *ink st+dents to !o+rses. St+dents are *inked to !o+rses by the St+dentGrades

    tab*e.L

    Another di3eren!e between the database diagram and the data mode* is the

    additiona* Na#igation P"ope"tiesse!tion -or ea!h entity. A na(igation )ro)erty o- an entity

    re-eren!es re*ated entities. For eam)*e# the &o+rses)ro)erty in a Person entity !ontains a

    !o**e!tion o- a** the &o+rse entities that are re*ated to that Person entity.

     

     Jet another di3eren!e between the database and data mode* is the absen!e o-

    the &o+rse/nstr+!tor asso!iation tab*e that=s +sed in the database to *ink

  • 8/16/2019 Getting Started With Entity Framework 4

    17/21

    1! | P a g e 0

    the Person and &o+rse tab*es in a manyMtoMmany re*ationshi). The na(igation )ro)erties

    enab*e yo+ to get re*ated &o+rse entities -rom the Person entity and re*ated Person entities

    -rom the &o+rse entity# so there=s no need to re)resent the asso!iation tab*e in the data

    mode*.

    For )+r)oses o- this t+toria*# s+))ose the FirstName !o*+mn o- the Person tab*e a!t+a**y

    !ontains both a )erson=s rst name and midd*e name. Jo+ want to !hange the name o- thee*d to ree!t this# b+t the database administrator KDBAL might not want to !hange the

    database. Jo+ !an !hange the name o- the FirstName )ro)erty in the data mode*# whi*e

    *ea(ing its database e;+i(a*ent +n!hanged.

    /n the designer# rightM!*i!k i"stNae in the Person entity# and then se*e!t enae.

  • 8/16/2019 Getting Started With Entity Framework 4

    18/21

    1' | P a g e 0

     Ty)e in the new name >First7idName>. This !hanges the way yo+ re-er to the !o*+mn in !ode

    witho+t !hanging the database.

  • 8/16/2019 Getting Started With Entity Framework 4

    19/21

    1) | P a g e 0

     The mode* browser )ro(ides another way to (iew the database str+!t+re# the data mode*str+!t+re# and the ma))ing between them. To see it# rightM!*i!k a b*ank area in the entity

    designer and then !*i!k -odel "o&se".

     The -odel "o&se" )ane dis)*ays a tree (iew. KThe -odel "o&se" )ane might be do!ked

    with the Sol*tion Explo"e" )ane.L The Sc(ool-odel node re)resents the data mode*

    str+!t+re# and the Sc(ool-odel.Sto"e node re)resents the database str+!t+re.

  • 8/16/2019 Getting Started With Entity Framework 4

    20/21

    2 | P a g e 0

    E)and Sc(ool-odel.Sto"e to see the tab*es# e)and Tables 7ie&s to see tab*es# and

    then e)and %o*"se to see the !o*+mns within a tab*e.

    E)and Sc(ool-odel# e)and Entit, T,pes# and then e)and the %o*"se node to see the

    entities and the )ro)erties within the entities.

    /n either the designer or the -odel "o&se" )ane yo+ !an see how the Entity Framework

    re*ates the ob9e!ts o- the two mode*s. OightM!*i!k the Person entity and se*e!t Table

    -apping.

  • 8/16/2019 Getting Started With Entity Framework 4

    21/21

    21 | P a g e 0

     This o)ens the -apping $etails window. Noti!e that this window *ets yo+ see that the

    database !o*+mnFirstName is ma))ed to First7idName# whi!h is what yo+ renamed it to in

    the data mode*.

     The Entity Framework +ses Q7< to store in-ormation abo+t the database# the data mode*#

    and the ma))ings between them. The SchoolModel.edmx  *e is a!t+a**y an Q7< *e that!ontains this in-ormation. The designer renders the in-ormation in a gra)hi!a* -ormat# b+t

    yo+ !an a*so (iew the *e as Q7< by rightM!*i!king the .edmx  *e in Sol*tion Explo"e"#!*i!king +pen Wit(# and se*e!ting 8-9 :Text; Edito". KThe data mode* designer and an

    Q7< editor are 9+st two di3erent ways o- o)ening and working with the same *e# so yo+

    !annot ha(e the designer o)en and o)en the *e in an Q7< editor at the same time.L

     Jo+=(e now !reated a website# a database# and a data mode*. /n the net wa*kthro+gh yo+=**

    begin working with data +sing the data mode* and the ASP.NET EntityDataSo+r!e !ontro*.