1 Getting Started with Entity Framework 6 Code First using MVC 5 Mon Jul 06, 2015 7:56 am
Admin
Admin
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.
A Visual Basic version translated by Mike Brind is available: MVC 5 with EF 6 in Visual Basic on the Mikesdotnetting site.
Software versions used in the tutorial
Tutorial versions
Questions and comments
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.
The UI style of this site has been kept close to what's generated by the built-in templates, so that the tutorial can focus mainly on how to use the Entity Framework.
Prerequisites
See Software Versions at the top of the page. Entity Framework 6 is not a prerequisite because you install the EF NuGet package as part of the tutorial.
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.
If the Host in the cloud check box in the Microsoft Azure section is selected, clear it.
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
A few simple changes will set up the site menu, layout, and home page.
Open Views\Shared\_Layout.cshtml, and make the following changes:
- Change each occurrence of "My ASP.NET Application" and "Application name" to "Contoso University".
- Add menu entries for Students, Courses, Instructors, and Departments, and delete the Contact entry.
The changes are highlighted.
@Styles.Render("~/Content/css")
@Scripts.Render("~/bundles/modernizr")
@RenderBody()
@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.
Press CTRL+F5 to run the site. You see the home page with the main menu.
Install Entity Framework 6
From the Tools menu click NuGet Package Manager and then click Package Manager Console.
In the Package Manager Console window enter the following command:
- Code:
Install-Package EntityFramework
The image shows 6.0.0 being installed, but NuGet will install the latest version of Entity Framework (excluding pre-release versions), which as of the most recent update to the tutorial is 6.1.1.
This step is one of a few steps that this tutorial has you do manually, but which could have been done automatically by the ASP.NET MVC scaffolding feature. You're doing them manually so that you can see the steps required to use the Entity Framework. You'll use scaffolding later to create the MVC controller and views. An alternative is to let scaffolding automatically install the EF NuGet package, create the database context class, and create the connection string. When you're ready to do it that way, all you have to do is skip those steps and scaffold your MVC controller after you create your entity classes.
Create the Data Model
Next you'll create entity classes for the Contoso University application. You'll start with the following three entities:
There's a one-to-many relationship between
- Code:
Student
- Code:
Enrollment
- Code:
Course
- Code:
Enrollment
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
}
}
The
- Code:
ID
- Code:
ID
- Code:
ID
The
- Code:
Enrollments
- Code:
Enrollments
- Code:
Student
- Code:
Enrollment
- Code:
Student
- Code:
Student
- Code:
Enrollment
- Code:
StudentID
- Code:
Student
- Code:
Enrollments
- Code:
Enrollment
Navigation properties are typically defined as
- Code:
virtual
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
- Code:
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
}
public class Enrollment
{
public int EnrollmentID { get; set; }
public int CourseID { get; set; }
public int StudentID { get; set; }
public Grade? Grade { get; set; }
public virtual Course Course { get; set; }
public virtual Student Student { get; set; }
}
}
The
- Code:
EnrollmentID
- Code:
ID
- Code:
ID
- Code:
Student
- Code:
ID
- Code:
classname
The
- Code:
Grade
- Code:
Grade
- Code:
Grade
The
- Code:
StudentID
- Code:
Student
- Code:
Enrollment
- Code:
Student
- Code:
Student
- Code:
Student.Enrollments
- Code:
Enrollment
The
- Code:
CourseID
- Code:
Course
- Code:
Enrollment
- Code:
Course
Entity Framework interprets a property as a foreign key property if it's named
- Code:
StudentID
- Code:
Student
- Code:
Student
- Code:
ID
- Code:
CourseID
- Code:
Course
- Code:
CourseID
The Course Entity
In the Models folder, create Course.cs, replacing the template code with the following code:
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace ContosoUniversity.Models
{
public class Course
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public virtual ICollection
}
}
The
- Code:
Enrollments
- Code:
Course
- Code:
Enrollment
We'll say more about the DatabaseGenerated attribute in a later tutorial in this series. Basically, this attribute lets you enter the primary key for the course rather than having the database generate it.
Create the Database Context
The main class that coordinates Entity Framework functionality for a given data model is the database context class. You create this class by deriving from the System.Data.Entity.DbContext class. In your code you specify which entities are included in the data model. You can also customize certain Entity Framework behavior. In this project, the class is named
- Code:
SchoolContext
To create a folder in the ContosoUniversity project, right-click the project in Solution Explorer and click Add, and then click New Folder. Name the new folder DAL (for Data Access Layer). In that folder create a new class file named SchoolContext.cs, and replace the template code with the following code:
using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace ContosoUniversity.DAL
{
public class SchoolContext : DbContext
{
public SchoolContext() : base("SchoolContext")
{
}
public DbSet
public DbSet
public DbSet
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove
}
}
}
Specifying entity sets
This code creates a DbSet property for each entity set. In Entity Framework terminology, an entity set typically corresponds to a database table, and an entity corresponds to a row in the table.
You could have omitted the
- Code:
DbSet<Enrollment>
- Code:
DbSet<Course>
- Code:
Student
- Code:
Enrollment
- Code:
Enrollment
- Code:
Course
Specifying the connection string
The name of the connection string (which you'll add to the Web.config file later) is passed in to the constructor.
public SchoolContext() : base("SchoolContext")
{
}
You could also pass in the connection string itself instead of the name of one that is stored in the Web.config file. For more information about options for specifying the database to use, see Entity Framework - Connections and Models.
If you don't specify a connection string or the name of one explicitly, Entity Framework assumes that the connection string name is the same as the class name. The default connection string name in this example would then be
- Code:
SchoolContext
Specifying singular table names
The
- Code:
modelBuilder.Conventions.Remove
- Code:
Students
- Code:
Courses
- Code:
Enrollments
- Code:
Student
- Code:
Course
- Code:
Enrollment
Set up EF to initialize the database with test data
The Entity Framework can automatically create (or drop and re-create) a database for you when the application runs. You can specify that this should be done every time your application runs or only when the model is out of sync with the existing database. You can also write a
- Code:
Seed
The default behavior is to create a database only if it doesn't exist (and throw an exception if the model has changed and the database already exists). In this section you'll specify that the database should be dropped and re-created whenever the model changes. Dropping the database causes the loss of all your data. This is generally OK during development, because the
- Code:
Seed
In the DAL folder, create a new class file named SchoolInitializer.cs and replace the template code with the
following code, which causes a database to be created when needed and loads test data into the new database.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using ContosoUniversity.Models;
namespace ContosoUniversity.DAL
{
public class SchoolInitializer : System.Data.Entity. DropCreateDatabaseIfModelChanges
{
protected override void Seed(SchoolContext context)
{
var students = new List
{
new Student{FirstMidName="Carson",LastName="Alexander",EnrollmentDate=DateTime.Parse("2005-09-01")},
new Student{FirstMidName="Meredith",LastName="Alonso",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Arturo",LastName="Anand",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Gytis",LastName="Barzdukas",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Yan",LastName="Li",EnrollmentDate=DateTime.Parse("2002-09-01")},
new Student{FirstMidName="Peggy",LastName="Justice",EnrollmentDate=DateTime.Parse("2001-09-01")},
new Student{FirstMidName="Laura",LastName="Norman",EnrollmentDate=DateTime.Parse("2003-09-01")},
new Student{FirstMidName="Nino",LastName="Olivetto",EnrollmentDate=DateTime.Parse("2005-09-01")}
};
students.ForEach(s => context.Students.Add(s));
context.SaveChanges();
var courses = new List
{
new Course{CourseID=1050,Title="Chemistry",Credits=3,},
new Course{CourseID=4022,Title="Microeconomics",Credits=3,},
new Course{CourseID=4041,Title="Macroeconomics",Credits=3,},
new Course{CourseID=1045,Title="Calculus",Credits=4,},
new Course{CourseID=3141,Title="Trigonometry",Credits=4,},
new Course{CourseID=2021,Title="Composition",Credits=3,},
new Course{CourseID=2042,Title="Literature",Credits=4,}
};
courses.ForEach(s => context.Courses.Add(s));
context.SaveChanges();
var enrollments = new List
{
new Enrollment{StudentID=1,CourseID=1050,Grade=Grade.A},
new Enrollment{StudentID=1,CourseID=4022,Grade=Grade.C},
new Enrollment{StudentID=1,CourseID=4041,Grade=Grade.B},
new Enrollment{StudentID=2,CourseID=1045,Grade=Grade.B},
new Enrollment{StudentID=2,CourseID=3141,Grade=Grade.F},
new Enrollment{StudentID=2,CourseID=2021,Grade=Grade.F},
new Enrollment{StudentID=3,CourseID=1050},
new Enrollment{StudentID=4,CourseID=1050,},
new Enrollment{StudentID=4,CourseID=4022,Grade=Grade.F},
new Enrollment{StudentID=5,CourseID=4041,Grade=Grade.C},
new Enrollment{StudentID=6,CourseID=1045},
new Enrollment{StudentID=7,CourseID=3141,Grade=Grade.A},
};
enrollments.ForEach(s => context.Enrollments.Add(s));
context.SaveChanges();
}
}
}
The
- Code:
Seed
that object to add new entities to the database. For each entity type, the code creates a collection of new
entities, adds them to the appropriate
- Code:
DbSet
necessary to call the
- Code:
SaveChanges
you locate the source of a problem if an exception occurs while the code is writing to the database.
To tell Entity Framework to use your initializer class, add an element to the
- Code:
entityFramework
The
- Code:
context type
- Code:
databaseinitializer type
- Code:
context
- Code:
disableDatabaseInitialization="true"
As an alternative to setting the initializer in the Web.config file is to do it in code by adding a
- Code:
Database.SetInitializer
- Code:
Application_Start
The application is now set up so that when you access the database for the first time in a given run of the
application, the Entity Framework compares the database to the model (your
- Code:
SchoolContext
Note: When you deploy an application to a production web server, you must remove or disable code that drops and re-creates the database. You'll do that in a later tutorial in this series.
Set up EF to use a SQL Server Express LocalDB database
LocalDB is a lightweight version of the SQL Server Express Database Engine. It's easy to install and configure, starts on demand, and runs in user mode. LocalDB runs in a special execution mode of SQL Server Express that enables you to work with databases as .mdf files. You can put LocalDB database files in the App_Data folder of a web project if you want to be able to copy the database with the project. The user instance feature in SQL Server Express also enables you to work with .mdf files, but the user instance feature is deprecated; therefore, LocalDB is recommended for working with .mdf files. In Visual Studio 2012 and later versions, LocalDB is installed by default with Visual Studio.
Typically SQL Server Express is not used for production web applications. LocalDB in particular is not recommended for production use with a web application because it is not designed to work with IIS.
In this tutorial you'll work with LocalDB. Open the application Web.config file and add a
- Code:
connectionStrings
- Code:
appSettings
The connection string you've added specifies that Entity Framework will use a LocalDB database named ContosoUniversity1.mdf. (The database doesn't exist yet; EF will create it.) If you wanted the database to be created in your App_Data folder, you could add
- Code:
AttachDBFilename=|DataDirectory|\ContosoUniversity1.mdf
You don't actually have to have a connection string in the Web.config file. If you don't supply a connection string, Entity Framework will use a default one based on your context class. For more information, see Code First to a New Database.
Creating a Student Controller and Views
Now you'll create a web page to display data, and the process of requesting the data will automatically trigger
the creation of the database. You'll begin by creating a new controller. But before you do that, build the project to make the model and context classes available to MVC controller scaffolding.
[list defaultattr=]
[*]Right-click the Controllers folder in Solution Explorer, select Add, and then click New Scaffolded Item.
[*]In the Add Scaffold dialog box, select MVC 5 Controller with views, using Entity Framework.
[*]In the Add Controller dialog box, make the following selections and then click Add:
- Model class: Student (ContosoUniversity.Models). (If you don't see this option in the drop-down list, build the project and try again.)
- Data context class: SchoolContext (ContosoUniversity.DAL).
- Controller name: StudentController (not StudentsController).
- Leave the default values for the other fields.
When you click Add, the scaffolder creates a StudentController.cs file and a set of views (.cshtml files) that work with the controller. In the future when you create projects that use Entity Framework you can also take advantage of some additional functionality of the scaffolder: just create your first model class, don't create a connection string, and then in the Add Controller box specify new context class. The scaffolder will create your
- Code:
DbContext
[*]Visual Studio opens the Controllers\StudentController.cs file. You see a class variable has been created that instantiates a database context object:
private SchoolContext db = new SchoolContext();
The
- Code:
Index
- Code:
Students
public ViewResult Index()
{
return View(db.Students.ToList());
}
The Student\Index.cshtml view displays this list in a table:
@Html.DisplayNameFor(model => model.LastName) | @Html.DisplayNameFor(model => model.FirstMidName) | @Html.DisplayNameFor(model => model.EnrollmentDate) | |
---|---|---|---|
@Html.DisplayFor(modelItem => item.LastName) | @Html.DisplayFor(modelItem => item.FirstMidName) | @Html.DisplayFor(modelItem => item.EnrollmentDate) | @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) |
|
|