1. 程式人生 > >(3) Models [ 教學 ] (使用visual studio) « 關於網路那些事...

(3) Models [ 教學 ] (使用visual studio) « 關於網路那些事...

Model 基本介紹

首先,在Models資料夾中,新增名為Movie的Class

並且開啟新增的檔案
修改內容:
Models/Movie.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication3.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; } } }

接下來,先停止 IIS Express

接著,按下列步驟新增一個名稱為 Movies 的 Controller

Controllers/MoviesController.cs

完成上步驟後
Visual Studio 就會自動產生含有 CRUD (create, read, update, and delete) 的功能以及view

如果這時直接訪問 http://localhost:port/movies
會得到A database operation failed while processing the request.的錯誤

這時,先停止執行 IIS Express
準備設定 database

點選任一個專案內的檔案頁簽,右鍵開啟對應資料夾

並且前往專案的資料夾根目錄位置

開啟命令視窗(cmd),前往該位置

各別執行下兩列指令

dotnet ef migrations add Initial
dotnet ef database update

接下來

開啟 IIS Express
前往 localhost:port/Movies,就會看到以下畫面


點選畫面中的create new
填寫資料後,create一筆新資料

成功送出後,就會在畫面中看到新增的資料

參考:


如果你喜歡我們的文章內容,請在這裡按個讚