🚀 Kosol.net

Published

- 2 min read

วิธีสร้าง Blazer Project ด้วย command line (CLI)

img of วิธีสร้าง Blazer Project ด้วย command line (CLI)

วิธีสร้าง Project

1. Run this command in terminal to create new Blazor project

   dotnet new blazor -ai -int Auto -au Individual -o MyBlazor

cd MyBlazor

2. ติดตั้ง SQL Server EF Core ที่ Server Project

   dotnet add package Microsoft.EntityFrameworkCore

dotnet add package Microsoft.EntityFrameworkCore.SqlServer

dotnet add package Microsoft.EntityFrameworkCore.Tools

3. เพิ่ม Connection String ที่ appsetting.json ที่ Server Project

   {
	"ConnectionStrings": {
		"DefaultConnection": "Server=MyServerName;Database=MyDatabaseName;User Id=MyUserName;Password=MyPassword;Encrypt=False;TrustServerCertificate=True;"
	}
	//...
}

4. เรียกใช้ SqlServerConnection ใน Program.cs ที่ Server Project

   builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
   builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

หมายเหตุ กรณีใช้ Security ของ AspNet ให้ migration และ update db ก่อน

Create Migration (สร้าง)

   dotnet ef migrations add sqlserver

Run script to Database (รัน script)

   dotnet ef database update

5. สร้าง Model Folder Data ที่ Server Project

   using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

[Table("MyTable")]
public class MyTable
{
    [Key]
    public long MyTableID { get; set; }
    ...
}

6. เพิ่ม DbSet ที่ ApplicationDbContext ที่ Folder Data

   using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : IdentityDbContext<ApplicationUser>(options)
{
}

ด้านล่างเป็นแบบไม่ได้ใช้ Security

   using Microsoft.EntityFrameworkCore;

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }

    public DbSet<Post> Post { get; set; }
}

7. สร้าง Repository ที่ Folder Services

7.1 สร้าง Interface ITimeSheetRepository

   public interface ITimeSheetRepository
{
    public Task<IEnumerable<TimeSheet>> GetTimeSheets(DateTime startDate, DateTime? endDate = null);
}

7.2 สร้าง Class TimeSheetRepository

   using Microsoft.EntityFrameworkCore;
using MyBlazor.Data;

public class TimeSheetRepository : ITimeSheetRepository
{
    private readonly ApplicationDbContext _context;

    public TimeSheetRepository(ApplicationDbContext context)
    {
        _context = context;
    }
    public async Task<IEnumerable<TimeSheet>> GetTimeSheets(DateTime startDate, DateTime? endDate = null)
    {
        var finishDate = (endDate ?? startDate).AddDays(1);
        return await _context.TimeSheet
            .Where(t => t.StartTime >= startDate && t.FinishTime < finishDate)
            .OrderBy(t => t.EmpCode)
            .ThenBy(t => t.StartTime)
            .Take(200)
            .ToListAsync();
    }
}

8. ลงทะเบียน TimeSheetService ที่ Program.cs

   ...
builder.Services.AddScoped<ITimeSheetRepository, TimeSheetRepository>();
...