In this section, you will add classes used to manage movies in the database. These classes are "models" in ASP. net mvc applications )".
You will use the. NET Framework Data Access Technology Entity Framework to define and use these model classes. Entity Framework (usually referred to as EF) is a development mode that supports Code First. Code first allows you to create an object model by writing simple classes. (Compared to the "original CLR objects", this is also called the POCO class), you can then create a database from your class, which is a very clean and fast development workflow. If you have to create a database first, you can still follow this tutorial to learn about MVC and EF application development. Then, you can follow the Scaffolding tutorial of Tom Fizmakens ASP. NET, which covers how to create a database first.
Add model class
InSolution Resource Manager, Right-clickModelFolder, selectAddAnd then selectClass.
InputClassThe name is "Movie ".
Add the following five attributesMovie
Class:
using System;namespace MvcMovie.Models{ public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } }}
We will useMovie
Class to represent movies in the database.Movie
Each instance of the object corresponds to a row in the database table, Movie
Each attribute of the class corresponds to a column in the table.
In the same file, add the followingMovieDBContext
Class:
using System;using System.Data.Entity;namespace MvcMovie.Models{ public class Movie { public int ID { get; set; } public string Title { get; set; } public DateTime ReleaseDate { get; set; } public string Genre { get; set; } public decimal Price { get; set; } } public class MovieDBContext : DbContext { public DbSet
Movies { get; set; } }}
MovieDBContext
Class represents the Movie Database Class of Entity Framework. This class is used to obtain, store, update, and process data in the database.Movie
Class.MovieDBContext
DbContext base class inherited from Entity Framework.
To be able to referenceDbContext
AndDbSet
To add the followingusing
Statement:
using System.Data.Entity;
To do this, you can manually add the using statement, or you can right-click the red wavy line, "Resolve", and then click"Using System. Data. Entity.
Note: Some unnecessary using statements have been deleted-right-click the file and select"Organization UsingAnd then click"Remove unused using".
So far, we have added a model (M in MVC). In the next section, you will use the database connection string.