Adding a model

Adding data model classes

In Solution Explorer, right click the Models folder > Add > Class. Name the class Movie and add the following properties:

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; }
    }
}

n addition to the properties you’d expect to model a movie, the ID field is required by the DB for the primary key.

Build the project.

If you don’t build the app, you’ll get an error in the next section.

We’ve finally added a Model to our MVC app.

Scaffolding a controller

In Solution Explorer, right-click the Controllers folder > Add > Controller.

In the Add Scaffold dialog, tap MVC Controller with views, using Entity Framework > Add.

Complete the Add Controller dialog

  • Model class: Movie(MvcMovie.Models)
  • Data context class: ApplicationDbContext(MvcMovie.Models)
  • Views:: Keep the default of each option checked
  • Controller name: Keep the default MoviesController
  • Tap Add

The Visual Studio scaffolding engine creates the following:

  • A movies controller (Controllers/MoviesController.cs)
  • Create, Delete, Details, Edit and Index Razor view files (Views/Movies)

Visual Studio automatically created the CRUD (create, read, update, and delete) action methods and views for you (the automatic creation of CRUD action methods and views is known as scaffolding).

You’ll soon have a fully functional web application that lets you create, list, edit, and delete movie entries.

If you run the app and click on the Mvc Movie link, you’ll get the following errors:

We’ll follow those instructions to get the database ready for our Movie app.

Update the database

Warning

You must stop IIS Express before you update the database.

To Stop IIS Express:

  • Right click the IIS Express system tray icon in the notification area
  • Tap Exit or Stop Site
  • Alternatively, you can exit and restart Visual Studio
  • Open a command prompt in the project directory (MvcMovie/src/MvcMovie). Follow these instructions for a quick way to open a folder in the project directory.

    • Open a file in the root of the project (for this example, use Startup.cs.)
    • Right click on Startup.cs > Open Containing Folder.
  • Shift + right click a folder > Open command window here
  • Run cd .. to move back up to the project directory
  • Run the following commands in the command prompt:
dotnet ef migrations add Initial
dotnet ef database update
时间: 2024-10-05 03:40:48

Adding a model的相关文章

【目录】开始使用ASP.NET Core MVC和Visual Studio

参照微软教程:Building your first ASP.NET Core MVC app with Visual Studio This series of tutorials will teach you the basics of building an ASP.NET Core MVC web app using Visual Studio. Getting started Adding a controller Adding a view Adding a model Workin

Getting Started with ASP.NET Web API 2 (C#)

By Mike Wasson|last updated May 28, 2015 7556 of 8454 people found this helpful Print Download Completed Project HTTP is not just for serving up web pages. It is also a powerful platform for building APIs that expose services and data. HTTP is simple

【ASP.NET Web API教程】1.1 第一个ASP.NET Web API

参考页面: http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-put.html http://www.yuanjiaocheng.net/webapi/mvc-consume-webapi-delete.html http://www.yuanjiaocheng.net/webapi/httpclient-consume-webapi.html http://www.yuanjiaocheng.net/webapi/webapi-di-

简单记录在Visual Studio 2013中创建ASP.NET Web API 2

在很多跨平台的应用中就需要Web API ,比如android与数据库的交互. Create a Web API Project 选择新建项目下的模板下的Visual C#节点下的Web节点,在模板列表下选择ASP.NET Web 应用程序,并命名为ChatApp就可以了. 在新建ASP.NET项目下选择Web API 点击确定就可以了. Adding a Model 在解决方案下新建一个Model //新建Model User namespace APP_Chat.Models { publi

webAPI---发布(IIS)--发布遇到问题(500.19,500.21,404.8,404.3)

HTTP is not just for serving up web pages. It is also a powerful platform for building APIs that expose services and data. HTTP is simple, flexible, and ubiquitous. Almost any platform that you can think of has an HTTP library, so HTTP services can r

[转]Getting Started with ASP.NET Web API 2 (C#)

http://www.asp.net/web-api 本文转自:http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api By          Mike WassonMike Wasson|January 20, 2014 Print Tweet Download Completed Project HTTP is not just for servin

MVC架构学习

作为一名小小的GreenBird,学习MVC呢,已经花费了2天了,期间得到了美丽的学姐的帮助,初步整理了一下. 首先,学习MVC呢就先以一个标准的MVC的简单的例子来入手,然后根据学姐的PPT,我用vs2012建立了一个项目. 1.建立一个MVC架构的项目: File->New Project->Visual C#->ASP.NET MVC 4 Web Application 可以直接运行这个网站 2.Adding a Controller 右键单击Controller->add-

SSIS教程:创建简单的ETL包 -- 5. 添加包部署模型的包配置(Adding Package Configurations for the Package Deployment Model)

包配置允许您从开发环境的外部设置运行时属性和变量. 配置允许您开发灵活且易于部署和分发的包.Microsoft Integration Services 提供了以下配置类型: XML 配置文件 环境变量 注册表项 父包变量 SQL Server 表 Step 1: 复制第 4 课包 Step 2: 启用和配置包配置 创建映射到 Directory 属性的新的包级别变量 在 SSIS 设计器中,单击“控制流”选项卡的背景. 这会将要创建的变量的作用域设置为包. 在 SSIS 菜单中,选择“变量”.

Adding an On/Off switch to your Raspberry Pi

http://www.raspberry-pi-geek.com/Archive/2013/01/Adding-an-On-Off-switch-to-your-Raspberry-Pi#article_f5 Which Switch? Aaron Shaw Pulling the plug on your Pi without an orderly shutdown can corrupt the SD card. Also, many users prefer a convenient sw