"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Adding a Model (VB)

by Rick Anderson

This tutorial will teach you the basics of building an ASP.NET MVC Web application using Microsoft Visual Web Developer 2010 Express Service Pack 1, which is a free version of Microsoft Visual Studio. Before you start, make sure you’ve installed the prerequisites listed below. You can install all of them by clicking the following link: Web Platform Installer. Alternatively, you can individually install the prerequisites using the following links:

If you’re using Visual Studio 2010 instead of Visual Web Developer 2010, install the prerequisites by clicking the following link: Visual Studio 2010 prerequisites.

A Visual Web Developer project with VB.NET source code is available to accompany this topic. Download the VB.NET version. If you prefer C#, switch to the C# version of this tutorial.

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 application.

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.

Adding Model Classes

In Solution Explorer, right click the Models folder, select Add, and then select Class.

Name the class “Movie”.

Add the following five properties to the Movie class:

[!code-vbMain]

   1:  Public Class Movie 
   2:          Public Property ID() As Integer 
   3:          Public Property Title() As String 
   4:          Public Property ReleaseDate() As Date 
   5:          Public Property Genre() As String 
   6:          Public Property Price() As Decimal 
   7:  End Class

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:

[!code-vbMain]

   1:  Public Class MovieDBContext
   2:      Inherits DbContext
   3:      Public Property Movies() As DbSet(Of Movie)
   4:  End Class

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. For more information about DbContext and DbSet, see Productivity Improvements for 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:

[!code-vbMain]

   1:  Imports System.Data.Entity

The complete Movie.vb file is shown below.

[!code-vbMain]

   1:  Imports System.Data.Entity
   2:   
   3:  Public Class Movie
   4:          Public Property ID() As Integer
   5:          Public Property Title() As String
   6:          Public Property ReleaseDate() As Date
   7:          Public Property Genre() As String
   8:          Public Property Price() As Decimal
   9:  End Class
  10:   
  11:  Public Class MovieDBContext
  12:      Inherits DbContext
  13:      Public Property Movies() As DbSet(Of Movie)
  14:  End Class

Creating a Connection String and Working with SQL Server Compact

The MovieDBContext class you created handles the task of connecting to the database and mapping Movie objects to database records. One question you might ask, though, is how to specify which database it will connect to. You’ll do that by adding connection information in the Web.config file of the application.

Open the application root Web.config file. (Not the Web.config file in the Views folder.) The image below show both Web.config files; open the Web.config file circled in red.

Add the following connection string to the <connectionStrings> element in the Web.config file.

[!code-xmlMain]

   1:  <add name="MovieDBContext" 
   2:           connectionString="Data Source=|DataDirectory|Movies.sdf" 
   3:           providerName="System.Data.SqlServerCe.4.0"/>

The following example shows a portion of the Web.config file with the new connection string added:

[!code-xmlMain]

   1:  <configuration>
   2:    <connectionStrings>
   3:      <add name="MovieDBContext" 
   4:           connectionString="Data Source=|DataDirectory|Movies.sdf" 
   5:           providerName="System.Data.SqlServerCe.4.0"/>
   6:      <add name="ApplicationServices"
   7:           connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
   8:           providerName="System.Data.SqlClient" />
   9:    </connectionStrings>

This small amount of code and XML is everything you need to write in order to represent and store the movie data in a database.

Next, you’ll build a new MoviesController class that you can use to display the movie data and allow users to create new movie listings.

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions/getting-started-with-aspnet-mvc3/vb/adding-a-model.htm
< THANKS ME>