How to Use Entity Framework Code First for MVC 3

download How to Use Entity Framework Code First for MVC 3

of 3

Transcript of How to Use Entity Framework Code First for MVC 3

  • 8/14/2019 How to Use Entity Framework Code First for MVC 3

    1/3

    How to use Entity Framework Code withMVC3ASP.NET MVC 3 is a framework for buidin! s"aabe# standards$based web

    a%%i"ations usin! we$estabished desi!n %atterns and the %ower of ASP.NET andthe .NET Framework.

    ASP.NET MVC aows software de&eo%ers to buid a web a%%i"ationas a "om%osition

    of three roes'

    Model

    View

    Controller.

    Some of the notabe features in this ASP.NET MVC in"ude'

    The (a)or View En!ine

    *m%ro&ed su%%ort for &aidation and +a&aS"ri%t

    Su%%ort for out%ut "a"hin!

    Enhan"ed su%%ort for de%enden"y in,e"tion

    Sessioness Controer Su%%ort'

    Chid A"tion -ut%ut Ca"hin!'

    (emoteAttribute'

    e%enden"y (eso&er'

    /hat is Entity Framework#

    Entity Framework is an -b,e"t (eationa Ma%%er 0-(M1. *t basi"ay!enerates business ob,e"ts and entities a""ordin! to the database tabes and

    %ro&ides the me"hanism for'

    Performin! basi" C(2 0Create# (ead# 2%date# eete1 o%erations.

    Easiy mana!in! 4 to 4# 4 to many# and many to many reationshi%s.

    Abiity to ha&e inheritan"e reationshi%s between entities

    The benefits are:

    We can have all data access logic written in higher level languages. The conceptual model can be represented in a better way by using

    relationships among entities.

    The underlying data store can be replaced without much overhead since all

    data access logic is present at a higher level.

    http://en.wikipedia.org/wiki/Web_applicationhttp://en.wikipedia.org/wiki/Web_application
  • 8/14/2019 How to Use Entity Framework Code First for MVC 3

    2/3

    2se Entity Framework with MVC3

    In Visual Studio 200! add a new pro"ect by selecting the #ile menu! then $ew and then %ro"ect.

    In the Search Installed Templates te&t bo& 'top right(! type )V* + to filter down the list

    of pro"ect templates.

    Select ,S%.$-T )V* + Web ,pplication using your language of choice. This walthrough

    will use */ code! but you can find both */ and Visual asic versions in the sample

    solutions download.

    $ame the pro"ect )odel #irst )V* and then clic 1.

    In the $ew ,S%.$-T )V* + %ro"ect wi3ard! choose Internet ,pplication.

    4eave the default values of the other options and clic 1.

    Creating the Model

    In Solution -&plorer! right clic the )odels folder.

    1pen Visual Studio 200! select 5#ile5 67 5$ew5 67 5%ro"ect...5. The $ew %ro"ect

    Window will open. Select the ,S%.$-T )V*+ Web ,pplication template! change the

    $ame! change the 4ocation if re8uired! then select 515. The $ew ,S%.$-T )V*+

    %ro"ect Window will open. Select 5-mpty template5 then press the 515 button.

    .1pen S94 Server! create a database named 5Test5! create a table named

    5employee5with columns such as *ode as $umeric! I as string! $ame as string and

    epartment as string.

    2.,fter creating the database! go to the Solution -&plorer of Visual Studio and right6

    clic on the model folder. Then go to 5,dd5 67 5$ew Item5. The ,dd $ew Item Window

    will open! select ,1.$-T -ntity ata )odel. ;ename the entity data model to test!

    than press 515.

  • 8/14/2019 How to Use Entity Framework Code First for MVC 3

    3/3

    +.Select 5 the

    selected table will appear in the test.edm& file.

    ?.,gain go to the Solution -&plorer! right6clic on the *ontrollers folder! clic 5,dd

    *ontroller5. In the ,dd *ontroller window rename the controller to 5-mpcontroler5!

    select the option 5,dd action for *reate! @pdate! elete and etail Scenario5. Then

    press 5,dd5.

    A.The -mp*ontroler class will appear in the Visual Studio. *reate the ob"ect of the

    entity class that you created when you created the model. The code is given below.

    B.uild the application! so the employee model class will appear while creating the

    view for your application.

    C.Then right6clic on the inde& function and select 5,dd View5! rename that view if

    re8uired. Select 5*reate a Strongly6typed View5 than select 5-mployee )odel5! then

    select Scaffold template as to list. Than press 5,dd5.

    D.,fter creating the view write the following code in the inde& function:

    4istE)odels.-mployee7 emp F T-.-mployees.To4ist'(

    0.,gain right6clic on the etail function and clic 5,dd view5! select scaffold as toetails. In the etails function write the following code:

    )odels.-mployeeemp F T-.-mployees.Where'& F7 &.*ode FF id(.Single'(>

    .In the post create function! lie in the preceding steps to create the

    view!

    !*reate a viewto edit and!*reate the view for the delete

    ;un the application and do a create! update and delete of your data.