MVC 5 Tutorial for beginners, Exercises of Mathematics

Getting started with entity framework 6 code first using mvc 5

Typology: Exercises

2016/2017

Uploaded on 07/20/2017

sthabiso-didi
sthabiso-didi 🇿🇦

1 document

1 / 292

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download MVC 5 Tutorial for beginners and more Exercises Mathematics in PDF only on Docsity!

Getting Started with Entity

Framework 6 Code First using MVC 5

Tom Dykstra, Rick Anderson

Summary : The Contoso University sample web application demonstrates how to create

ASP.NET MVC 5 applications using the Entity Framework 6, Code First workflow. This

tutorial shows how to build the application using Visual Studio 2013.

Category: Step-by-Step, Guide

Applies to : Entity Framework 6, MVC 5, Visual Studio 2013

Source : ASP.NET (http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-

mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application)

E-book publication date : April, 2014

For more titles, visit the E-Book Gallery for Microsoft Technologies.

Table of Contents

Creating an Entity Framework Data Model

Download Completed Project

Introduction

The Contoso University sample web application demonstrates how to create ASP.NET MVC 5

applications using the Entity Framework 6 and Visual Studio 2013. This tutorial uses the Code

First workflow. For information about how to choose between Code First, Database First, and

Model First, see Entity Framework Development Workflows.

The sample application is a web site for a fictional Contoso University. It includes functionality

such as student admission, course creation, and instructor assignments. This tutorial series

explains how to build the Contoso University sample application. You can download the

completed application.

Software versions used in the tutorial

 Visual Studio 2013

 .NET 4.

 Entity Framework 6 (EntityFramework 6.1.0 NuGet package)

 Windows Azure SDK 2.2 (or later, for the optional Azure deployment steps)

The tutorial should also work with Visual Studio 2013 Express for Web or Visual Studio 2012.

The VS 2012 version of the Windows Azure SDK is required for Windows Azure deployment

with Visual Studio 2012.

Tutorial versions

For previous versions of this tutorial, see the EF 4.1 / MVC 3 e-book and Getting Started with

EF 5 using MVC 4.

Questions and comments

Please leave feedback on how you liked this tutorial and what we could improve in the

comments at the bottom of the pages in the version of this tutorial on the ASP.NET site. If you

have questions that are not directly related to the tutorial, you can post them to the ASP.NET

Entity Framework forum, the Entity Framework and LINQ to Entities forum, or

StackOverflow.com.

If you run into a problem you can’t resolve, you can generally find the solution to the problem by

comparing your code to the completed project that you can download. For some common errors

and how to solve them, see Common errors, and solutions or workarounds for them.

The Contoso University Web Application

The application you'll be building in these tutorials is a simple university web site.

Users can view and update student, course, and instructor information. Here are a few of the

screens you'll create.

Create an MVC Web Application

Open Visual Studio and create a new C# Web project named "ContosoUniversity".

In the New ASP.NET Project dialog box select the MVC template.

Click Change Authentication.

In the Change Authentication dialog box, select No Authentication , and then click OK. For

this tutorial you won't be requiring users to log on or restricting access based on who's logged on.

Back in the New ASP.NET Project dialog box, click OK to create the project.

Set Up the Site Style

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false)

In Views\Home\Index.cshtml , replace the contents of the file with the following code to replace

the text about ASP.NET and MVC with text about this application:

@{ ViewBag.Title = "Home Page"; }

Contoso University

Welcome to Contoso University

Contoso University is a sample application that demonstrates how to use Entity Framework 6 in an ASP.NET MVC 5 web application.

Build it from scratch

You can build the application by following the steps in the tutorial series on the ASP.NET site.

See the tutorial »

Download it

You can download the completed project from the Microsoft Code Gallery.

Download »

Press CTRL+F5 to run the site. You see the home page with the main menu.

There's a one-to-many relationship between Student and Enrollment entities, and there's a one-

to-many relationship between Course and Enrollment entities. In other words, a student can be

enrolled in any number of courses, and a course can have any number of students enrolled in it.

In the following sections you'll create a class for each one of these entities.

Note If you try to compile the project before you finish creating all of these entity classes, you'll

get compiler errors.

The Student Entity

In the Models folder, create a class file named Student.cs and replace the template code with the

following code:

using System; using System.Collections.Generic;

namespace ContosoUniversity.Models { public class Student { public int ID { get; set; } public string LastName { get; set; }

public string FirstMidName { get; set; } public DateTime EnrollmentDate { get; set; } public virtual ICollection Enrollments { get; set; } } }

The ID property will become the primary key column of the database table that corresponds to

this class. By default, the Entity Framework interprets a property that's named ID or classname ID

as the primary key.

The Enrollments property is a navigation property. Navigation properties hold other entities

that are related to this entity. In this case, the Enrollments property of a Student entity will

hold all of the Enrollment entities that are related to that Student entity. In other words, if a

given Student row in the database has two related Enrollment rows (rows that contain that

student's primary key value in their StudentID foreign key column), that Student entity's

Enrollments navigation property will contain those two Enrollment entities.

Navigation properties are typically defined as virtual so that they can take advantage of certain

Entity Framework functionality such as lazy loading. (Lazy loading will be explained later, in the

Reading Related Data tutorial later in this series.)

If a navigation property can hold multiple entities (as in many-to-many or one-to-many

relationships), its type must be a list in which entries can be added, deleted, and updated, such as

ICollection.

The Enrollment Entity

In the Models folder, create Enrollment.cs and replace the existing code with the following code:

namespace ContosoUniversity.Models { public enum Grade { A, B, C, D, F }