คำสั่งของ .NET Core

  1. ตรวจสอบเวอร์ชันของ .NET Core
  2. สร้างโปรเจ็กส์ด้วยคำสั่ง dotnet new
  3. รันโปรเจ็กส์ด้วยคำสั่ง dotnet run
  4. ดู info
  5. ติดตั้ง dotnet-ef
  6. อัพเดท dotnet tool

1.ตรวจสอบเวอร์ชันของ .NET Core

dotnet [--version] [--info] [--list-runtimes] [--list-sdks]

dotnet --help
> dotnet --version
6.0.302
> dotnet --info
> dotnet --list-sdks
5.0.410 [C:\Program Files\dotnet\sdk]
6.0.302 [C:\Program Files\dotnet\sdk]
> dotnet --list-runtimes
Microsoft.AspNetCore.App 3.1.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 3.1.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.27 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

2.สร้างโปรเจ็กส์ด้วยคำสั่ง dotnet new

สร้างโปรเจ็กส์ Console application

> dotnet new console -o Console1

สร้างโปรเจ็กส์ ASP.NET Core Web App

> dotnet new webapp -o WebApp1

หรือ

> dotnet new razor -o WebApp2

สร้างโปรเจ็กส์ ASP.NET Core Web App (MVC)

> dotnet new mvc -o WebMvc1

สร้างโปรเจ็กส์ Blazor

> dotnet new blazorserver -o BlazorApp --no-https

แต่ถ้าต้องการสร้างโปรเจ็กส์โดยระบุเวอร์ชันของ .Net ใช้คำสั่ง

> dotnet new console -f net5.0 -o Console5

โดยเลือกได้ดังนี้

  net5.0          - Target net5.0
  net6.0          - Target net6.0
  netcoreapp3.1   - Target netcoreapp3.1

3.รันโปรเจ็กส์ด้วยคำสั่ง dotnet run

> cd <PROJECT_FOLDER>
> dotnet run

หรือถ้าจะแก้ไขหน้า Page แล้ว refresh ดูผลลัพธ์ได้เลย ให้ใช้คำสั่ง (ดู Develop ASP.NET Core apps using a file watcher)

> dotnet watch run

4.ดู info

> dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.302
 Commit:    c857713418

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.302\

global.json file:
  Not found

Host:
  Version:      6.0.7
  Architecture: x64
  Commit:       0ec02c8c96

.NET SDKs installed:
  5.0.410 [C:\Program Files\dotnet\sdk]
  6.0.302 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.27 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.27 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.27 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

5.ติดตั้ง dotnet-ef

.net core 3.0 ไม่มีคำสั่งนี้มาให้

> dotnet ef migrations add Initial
Could not execute because the specified command or file was not found.
Possible reasons for this include:
  * You misspelled a built-in dotnet command.
  * You intended to execute a .NET Core program, but dotnet-ef does not exist.
  * You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.

ให้ติดตั้งก่อนใช้งาน ดู Entity Framework Core tools reference – .NET CLI

> dotnet tool install --global dotnet-ef
You can invoke the tool using the following command: dotnet-ef
Tool 'dotnet-ef' (version '3.0.0') was successfully installed.

ตรวจสอบว่าติดตั้งเรียบร้อยมั๊ย

> dotnet ef

                     _/\__
               ---==/    \\
         ___  ___   |.    \|\
        | __|| __|  |  )   \\\
        | _| | _|   \_/ |  //|\\
        |___||_|       /   \\\/\\

Entity Framework Core .NET Command-line Tools 3.0.0

ติดตั้งแพจเกจ Microsoft.EntityFrameworkCore.Design ล่าสุด

> dotnet add package Microsoft.EntityFrameworkCore.Design

Tutorial: Using the migrations feature – ASP.NET MVC with EF Core

> dotnet ef migrations add Initial
> dotnet ef database update

6.อัพเดท dotnet tool

ถ้ารันคำสั่ง dotnet แล้วได้ข้อความ “The EF Core tools version ‘3.0.0’ is older than that of the runtime ‘3.1.2’. Update the tools for the latest features and bug fixes.” ให้ทำการอัพเดท dotnet tool ด้วยคำสั่ง

> dotnet tool update --global dotnet-ef