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

Adding a Model

by Rick Anderson

[!INCLUDETutorial Note]

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 (xref:)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:

[!code-csharpMain]

   1:  using System;
   2:   
   3:  namespace MvcMovie.Models
   4:  {
   5:      public class Movie
   6:      {
   7:          public int ID { get; set; }
   8:          public string Title { get; set; }
   9:          public DateTime ReleaseDate { get; set; }
  10:          public string Genre { get; set; }
  11:          public decimal Price { get; set; }
  12:      }
  13:  }

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.

Note: In order to use System.Data.Entity, and the related class, you need to install the Entity Framework NuGet Package. Follow the link for further instructions.

In the same file, add the following MovieDBContext class:

[!code-csharpMain]

   1:  using System;
   2:  using System.Data.Entity;
   3:   
   4:  namespace MvcMovie.Models
   5:  {
   6:      public class Movie
   7:      {
   8:          public int ID { get; set; }
   9:          public string Title { get; set; }
  10:          public DateTime ReleaseDate { get; set; }
  11:          public string Genre { get; set; }
  12:          public decimal Price { get; set; }
  13:      }
  14:   
  15:      public class MovieDBContext : DbContext
  16:      {
  17:          public DbSet<Movie> Movies { get; set; }
  18:      }
  19:  }

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 using statement at the top of the file:

[!code-csharpMain]

   1:  using System.Data.Entity;

You can do this by manually adding the using statement, or you can hover over the red squiggly lines, click Show potential fixes and click using System.Data.Entity;

Note: Several unused using statements have been removed. Visual Studio will show unused dependencies as gray. You can remove unnused dependencies by hovering over the gray dependencies, click Show potential fixes and click Remove Unused Usings.

We’ve finally added a model (the M in MVC). In the next section you’ll work with the database connection string.

Previous Next



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