Adding a Model
来源:互联网 发布:tcp ip七层网络协议 编辑:程序博客网 时间:2024/06/05 18:33
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 calledCode 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, selectAdd, and then select Class.
Name the class "Movie".
Add the following five properties to the Movie
class:
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'll use the Movie
class to represent movies in a database. Each instance of aMovie
object will correspond to a row within a database table, and each property of theMovie
class will map to a column in the table.
In the same file, add the following MovieDBContext
class:
public class MovieDBContext : DbContext { public DbSet<Movie> Movies { get; set; } }
The MovieDBContext
class represents the Entity Framework movie database context, which handles fetching, storing, and updatingMovie
class instances in a database. The MovieDBContext
derives from theDbContext
base class provided by the Entity Framework. For more information aboutDbContext
and DbSet
, see Productivity Improvements for the Entity Framework.
In order to be able to reference DbContext
and DbSet
, you need to add the followingusing
statement at the top of the file:
using System.Data.Entity;
The complete Movie.cs file is shown below.
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<Movie> Movies { get; set; } }}
Creating a Connection String and Working with SQL Server Compact
The MovieDBContext
class you created handles the task of connecting to the database and mappingMovie
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 theWeb.config file of the application.
Open the application root Web.config file. (Not the Web.config file in theViews folder.) The image below show both Web.config files; open theWeb.config file circled in red.
Add the following connection string to the <connectionStrings>
element in theWeb.config file.
<add name="MovieDBContext" connectionString="Data Source=|DataDirectory|Movies.sdf" providerName="System.Data.SqlServerCe.4.0"/>
The following example shows a portion of the Web.config file with the new connection string added:
<configuration> <connectionStrings> <add name="MovieDBContext" connectionString="Data Source=|DataDirectory|Movies.sdf" providerName="System.Data.SqlServerCe.4.0"/> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> </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.
- Adding a Model
- Adding a New Field to the Movie Model and Table
- ASP.NET MVC 3 (Adding a New Field to the Movie Model and Table) (8/9)
- Adding Validation to the Model
- yocto: adding a package
- A. Adding Digits
- Adding a Controller (C#)
- Adding a View
- Adding a frame (C++)
- Adding a CoAP app
- RequireJS Adding a Library
- Adding a shadow around panel
- Adding A System Call(转)
- Adding Content to a Document
- Adding a handler in AXIS2
- Ubuntu:Adding a software repository
- Codeforces 260A Adding Digits
- Adding a simple io device
- 各种字符的英语写法
- note : when FSD HOOK + IRP_MJ_CREATE, judge pFileObject->Flags
- Intro to ASP.NET MVC 3 (C#)
- Adding a Controller (C#)
- Adding a View
- Adding a Model
- Accessing your Model's Data from a Controller
- Examining the Edit Methods and Edit View
- Adding a New Field to the Movie Model and Table
- Adding Validation to the Model
- Improving the Details and Delete Methods
- silverlight 遇到对 COM 组件的调用返回了错误 HRESULT E_FAIL
- Assignment 1
- Assignment 2