สร้างโปรเจ็กส์แบบ WebApi (ด้วย Visual Studio 2017)
เพิ่มโมเดล ApplicationUser และ Seed ข้อมูลให้กับโปรเจ็กส์ WebApi
จากนั้นมาปรับแต่งค่าตามนี้
โปรเจ็กส์แบบ WebApi นี้ ทำการคอนฟิกค่าเกี่ยวกับดาต้าเบสไว้ที่ Startup.cs
แต่ทำการ Seed ที่ Program.cs
สร้างโปรเจ็กส์แบบ WebApi (ด้วย Visual Studio 2017)
สร้างโฟลเดอร์ Data
สร้างโฟลเดอร์ Data\Models
สร้างคลาส Data\Models\ApplicationUser.cs
สร้างคลาส Data\SeedData.cs
Data\SeedData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using WebApi.Data.Models; namespace WebApi.Data { public static class SeedData { public static void Initialize(IServiceProvider serviceProvider) { using (var context = new ApplicationDbContext( serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>())) { // Look for any movies. if (context.Movie.Any()) { return; // DB has been seeded } context.Movie.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-2-12"), Genre = "Romantic Comedy", Price = 7.99M }, new Movie { Title = "Ghostbusters ", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M }, new Movie { Title = "Ghostbusters 2", ReleaseDate = DateTime.Parse("1986-2-23"), Genre = "Comedy", Price = 9.99M }, new Movie { Title = "Rio Bravo", ReleaseDate = DateTime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M } ); context.SaveChanges(); } } } } |
บรรทัดที่ 9 : SeedData
เป็น Static คลาส
บรรทัดที่ 11 : Initialize()
เป็น Static ฟังก์ชัน
บรรทัดที่ 17 : ตรวจสอบว่าตาราง Movie มีข้อมูลมั๊ย ถ้ามีก็ไม่ต้อง Seed
สร้างโปรเจ็กส์แบบ WebApi (ด้วย Visual Studio 2017)
สร้างโฟลเดอร์ Data
สร้างโฟลเดอร์ Data\Models
สร้างคลาส Data\Models\Movie.cs
Data\Models\Movie.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; namespace WebApp.Data.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; } } } |
สร้างโปรเจ็กส์ใหม่ ตั้งชื่อ WebApi
ASP.NET Core Web Application
เลือก API และไม่เลือก Configure for HTTPS
สร้างคลาส Data\SeedData.cs
Data\SeedData.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Linq; using WebApp.Data.Models; using WebApp.Models; namespace WebApp.Data { public static class SeedData { public static void Initialize(IServiceProvider serviceProvider) { using (var context = new ApplicationDbContext( serviceProvider.GetRequiredService<DbContextOptions<ApplicationDbContext>>())) { // Look for any movies. if (context.Movie.Any()) { return; // DB has been seeded } context.Movie.AddRange( new Movie { Title = "When Harry Met Sally", ReleaseDate = DateTime.Parse("1989-2-12"), Genre = "Romantic Comedy", Price = 7.99M }, new Movie { Title = "Ghostbusters ", ReleaseDate = DateTime.Parse("1984-3-13"), Genre = "Comedy", Price = 8.99M }, new Movie { Title = "Ghostbusters 2", ReleaseDate = DateTime.Parse("1986-2-23"), Genre = "Comedy", Price = 9.99M }, new Movie { Title = "Rio Bravo", ReleaseDate = DateTime.Parse("1959-4-15"), Genre = "Western", Price = 3.99M } ); context.SaveChanges(); } } } } |
บรรทัดที่ 10 : SeedData
เป็น Static คลาส
บรรทัดที่ 12 : Initialize()
เป็น Static ฟังก์ชัน
บรรทัดที่ 18 : ตรวจสอบว่าตาราง Movie มีข้อมูลมั๊ย ถ้ามีก็ไม่ต้อง Seed
สร้างโปรเจ็กส์แบบ WebApp (ด้วย Visual Studio 2017)
สร้างโฟลเดอร์ Data
สร้างโฟลเดอร์ Data\Models
สร้างคลาส Data\Models\Movie.cs
Data\Models\Movie.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; namespace WebApp.Data.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; } } } |
สร้างโปรเจ็กส์แบบ webapi
สร้างโปรเจ็กส์แบบ webapi ชื่อโปรเจ็กส์ webJwt
[code]
> dotnet new webapi -o webJwt
> cd webJwt
[/code]
สร้างโปรเจ็กส์แบบ webapi
สร้างโปรเจ็กส์แบบ webapi ชื่อโปรเจ็กส์ webApi
[code]
> dotnet new webapi -o webApi
> cd webApi
[/code]
*อันนี้เป็นโปรเจ็กส์แบบ webApi นะ ไม่ใช่ webApp
จะได้ไฟล์ Program.cs, Startup.cs, webApp.csproj, appsettings.json, Properties/launchSettings.json
สร้างโปรเจ็กส์แบบ Web > ASP.NET Core Web Application
Name: TestMakerFreeWebApp
Solution name: TestMakerFree
เลือก
Angular