asp.net mvc4 with entity framework5

Upload: apextgi

Post on 02-Jun-2018

236 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    1/64

    MVC 4 UsingEntity Framework 5

    Without Writing Code

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    2/64

    Introduction

    This article describes how to er!orm basic C"U# oerations in an MVC4 alication using Entity Framework 5without writing a single line o! code$ Entity Framework and MVC ha%e ad%anced to the le%el that we don&t need tout e!!ort into doing e'tra work$

    MVC Model( The business entity on which the o%erall alication oerates$ Many alications use a ersistentstorage mechanism )such as a database* to store data$ MVC does not seci!ically mention the data access layerbecause it is understood to be encasulated by the Model$

    View( The user inter!ace that renders the model into a !orm o! interaction$

    Controller( +andles a re,uest !rom a %iew and udates the model that results in a change in the Model&sstate$

    To imlement MVC in $-ET we need mainly three classes )View. Controller and the Model*$

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    3/64

    Entity FrameworkLet's have a look at the standard definition of Entity Framework given by Microsoft:

    "The Microsoft AD!ET Entity Framework is an b#ect$%elational Ma&&ing %M( framework thatenables develo&ers to work with relational data as domain)s&ecific ob#ects* eliminating the need for mostof the data access &l+mbing code that develo&ers +s+ally need to write! ,sing the Entity Framework*develo&ers iss+e -+eries +sing L./* then retrieve and mani&+late data as strongly ty&ed ob#ects! The

    Entity Framework's %M im&lementation &rovides services like change tracking* identity resol+tion* la0yloading* and -+ery translation so that develo&ers can foc+s on their a&&lication)s&ecific b+siness logicrather than the data access f+ndamentals!"

    .n sim&le terms* Entity Framework is an b#ect$%elational Ma&&ing %M( framework! .t is anenhancement to AD!ET* an +&&er layer to AD!et that gives develo&ers an a+tomated mechanism foraccessing and storing the data in the database!

    1o&e this &rovides a glim&se at an %M and EntityFramework!

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    4/64

    MVC /lication0te 1( Create a database named 23earning2 and add a table named 2student2 to it. the scrit o! the table is as !ollows(C"E/TET/63E 7dbo8$70tudent8) 70tudentId8 7n%archar8)19*-T -U33. 7First-ame8 7n%archar8)59*-U33. 73ast-ame8 7n%archar8)59*-U33. 7/ge8 7int8-U33. 7:ender8 7n%archar8)59*-U33. 76atch8 7n%archar8)59*-U33. 7/ddress8 7n%archar8)59*-U33. 7Class8 7n%archar8)59*-U33.

    70chool8 7n%archar8)59*-U33. 7#omicile8 7n%archar8)59*-U33.C-0T"/I-T 7;

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    5/64

    0te @( en your Visual 0tudio )the Visual 0tudio Version should be greaterthan or e,ual to 1@* and add an MVC internet alication as in the !ollowing(

    I ha%e gi%en it the name 2EntityWithMVC42$

    0te D( =ou&ll get a !ully structured MVC alication with de!ault +omecontroller in the Controller !older$ 6y de!ault Entity Framework is downloadedas a ackage in the alication !older. but i! not then you can add the EntityFramework ackage by rightclicking the roect. select 2Manage nuggetackages2 and search and install the Entity Framework$

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    6/64

    0te 4("ightclick the roect !ile. select 2/dd a new item2 and add the /# $-et Entity #ata Model. !ollow the rocedure inthe wiGard$

    :enerate a model !rom the database. select your ser%er and 23earning2 database name. the connection string will automaticallybe added to your Web$Con!ig. name that connection string 23earningEntities2$

    0elect tables to be added to the model$ In our case it&s the 20tudent2 table$

    0te 5( -ow add a new controller to the Controller !older. rightclick the controller !older and add a controller named 20tudent2$0ince we ha%e already created our data model. we can choose !or an otion where C"U# actions are created by the chosen EntityFramework data model$

    -ame your controller as 20tudentController2.From the 0ca!!olding tions. select 2MVC controller with readHwrite actions and %iews. using Entity Framework2$0elect the Model class as 20tudent2 that lies in our solution$0elect the #ata Conte't class as 23earningEntities2 that is added to our solution when we added the Entity Framework datamodel$0elect "aGor as the rendering engine !or %iews$

    Click 2/d%anced otions2. select 23ayout or master age2 and select 2

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    7/64

    0te B(We see out student controller reared with all the C"U# oeration actions as shown below(

    using 0ystemusing 0ystem$Collections$:enericusing 0ystem$#atausing 0ystem$#ata$Entityusing 0ystem$3in,using 0ystem$Webusing 0ystem$Web$M%cnamesace nockoutWithMVC4$ControllersJ

    ublicclass0tudentController ( Controller J ri%ate 3earningEntities db ? new 3earningEntities)*

    HH HH :ET( H0tudentH ublic /ction"esult Inde')* J return View)db$0tudents$To3ist)** K

    ublic /ction"esult #etails)string id ? null*

    J 0tudent student ? db$0tudents$Find)id* i! )student ?? null* J return +tt-otFound)* K return View)student* K

    ublic /ction"esult Create)*

    J return View)* K

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    8/64

    7+tt;ost8 7Validate/ntiForgeryToken8

    ublic /ction"esult Edit)0tudent student* J i! )Model0tate$IsValid* J db$Entry)student*$0tate ? Entity0tate$Modi!ied db$0a%eChanges)* return "edirectTo/ction)2Inde'2* K return View)student* K

    HH HH :ET( H0tudentH#eleteH5

    ublic /ction"esult #elete)string id ? null* J

    0tudent student ? db$0tudents$Find)id* i! )student ?? null* J return +tt-otFound)* K return View)student* K

    HH HH ;0T( H0tudentH#eleteH5

    7+tt;ost. /ction-ame)2#elete2*8 7Validate/ntiForgeryToken8 ublic /ction"esult #eleteCon!irmed)string id* J 0tudent student ? db$0tudents$Find)id*

    db$0tudents$"emo%e)student* db$0a%eChanges)* return "edirectTo/ction)2Inde'2* K

    rotectedo%erride%oid #isose)bool disosing* J db$#isose)* base$#isose)disosing* K KK

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    9/64

    0te ( &en the "A&&23tart" folder and change the name of the controller from"1ome" to "3t+dent"!

    The code will become:

    &+blicstaticvoid %egister%o+tes%o+te4ollection ro+tes(5

    ro+tes!.gnore%o+te"5reso+rce6!a7d$58&ath.nfo6"(9ro+tes!Ma&%o+te

    name: "Defa+lt"* +rl: "5controller6$5action6$5id6"* defa+lts: new 5 controller "3t+dent"* action ".nde7"* id ,rl;arameter!&tional 6(96

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    10/64

    0te A( ow &ress F< to r+n the a&&lication* and yo+'ll see the list of all st+dents we

    added into the 3t+dent table while creating it as dis&layed! 3ince the 4%,D o&erationsare a+tomatically written* we have action res+lts for a dis&lay list and other edit* deleteand create o&erations! ote that views for all the o&erations are created in the "=iews"folder +nder the "3t+dent" folder name!

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    11/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    12/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    13/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    14/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    15/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    16/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    17/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    18/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    19/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    20/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    21/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    22/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    23/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    24/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    25/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    26/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    27/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    28/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    29/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    30/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    31/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    32/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    33/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    34/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    35/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    36/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    37/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    38/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    39/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    40/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    41/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    42/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    43/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    44/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    45/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    46/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    47/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    48/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    49/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    50/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    51/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    52/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    53/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    54/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    55/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    56/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    57/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    58/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    59/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    60/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    61/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    62/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    63/64

  • 8/11/2019 Asp.Net MVC4 With Entity Framework5

    64/64