This tutorial series will teach you the basics of building an ASP.NET MVC 5 Web application using Visual Studio 2013 and Visual Basic. A Visual Studio Express For Web project with VB source code is available to accompany this series which you can download.
The tutorial series comprises 11 sections in total. They cover the basics of web development using the ASP.NET MVC framework and the Entity Framework for data access. They are intended to be followed sequentially as each section builds on the knowledge imparted in the previous sections. The navigation path through the series is as follows:
- Getting Started
- Adding a Controller
- Adding a View
- Adding a Model
- Creating a Connection String and Working with SQL Server LocalDB
- Accessing Your Model's Data from a Controller
- Examining the Edit Methods and Edit View
- Adding Search
- Adding a New Field
- Adding Validation
- Examining the Details and Delete Methods
4. Adding a Model
In this section you'll add some classes for managing movies in a database. These classes will be the "model" part of the ASP.NET MVC app.
You’ll use a .NET Framework data-access technology known as the Entity Framework to define and work with these model classes. The Entity Framework (often referred to as EF) supports a development paradigm called Code First. Code First allows you to create model objects by writing simple classes. (These are also known as POCO classes, from "Plain Old CLR Objects.") You can then have the database created on the fly from your classes, which enables a very clean and rapid development workflow. If you are required to create the database first, you can still follow this tutorial to learn about MVC and EF app development. You can then follow Tom Fizmakens ASP.NET Scaffolding tutorial, which covers the database first approach.
Adding Model Classes
In Solution Explorer, right click the Models folder, select Add, and then select Class.
Enter the class name "Movie".
Add the following five properties to the Movie class:
Namespace Models Public Class Movie Public Property ID As Integer Public Property Title As String Public Property ReleaseDate As DateTime Public Property Genre As String Public Property Price As Decimal End Class End Namespace
We'll use the Movie class to represent movies in a database. Each instance of a Movie object will correspond to a row within a database table, and each property of the Movie class will map to a column in the table.
In the same file, add the following MovieDBContext class:
Imports System.Data.Entity Namespace Models Public Class Movie Public Property ID As Integer Public Property Title As String Public Property ReleaseDate As DateTime Public Property Genre As String Public Property Price As Decimal End Class Public Class MovieDbContext Inherits DbContext Public Property Movies As DbSet(Of Movie) End Class End Namespace
The MovieDBContext class represents the Entity Framework movie database context, which handles fetching, storing, and updating Movie class instances in a database. The MovieDBContext derives from the DbContext base class provided by the Entity Framework.
In order to be able to reference DbContext and DbSet, you need to add the following Imports statement at the top of the file:
Imports System.Data.Entity
You can do this by manually typing the Imports statement, or you can click on the blue squiggly line under DbContext, press Shift + Alt + F10 and choose Import 'System.Data.Entity' from the context menu.
We've finally added a model (the M in MVC). In the next section you'll work with the database connection string.