Lớp học bình dân - Học online

Video

Thống kê lượt truy cập

Các bước lập trình web với Asp.net MVC core CodeFirst - Dự án Ký gửi hàng thời trang - Phần 1

Date: 04/04/2025 10:03 Author: Trinh Van Thanh Views: 57

Dưới đây là hướng dẫn tạo mô hình Code First cho chương trình quản lý thời trang có tích hợp ký gửi, giao hàng và thanh toán điện tử, sử dụng Entity Framework Core, C#SQL Server. Tên cơ sở dữ liệu sẽ là: kygui

Sử dụng app Visual Studio 2022

Bước 1: Tạo Project ASP.NET Core

Bước 2: Cài đặt Entity Framework Core

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools

Bước 3: Tạo DbContext & Cấu hình

using Microsoft.EntityFrameworkCore;

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

    public DbSet<Delivery> Deliveries { get; set; }
    public DbSet<OnlinePayment> OnlinePayments { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Shipper> Shippers { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // Thêm các ràng buộc (nếu có)
    }
}

Bước 4: Thêm các lớp thực thể.

Tạo tệp: KyGuiDbContext.cs thêm các lệnh sau

public class Delivery
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public Order Order { get; set; }

    public int ShipperId { get; set; }
    public Shipper Shipper { get; set; }

    public string DeliveryStatus { get; set; }
    public string DeliveryAddress { get; set; }
    public DateTime DeliveryTime { get; set; }
    public string Notes { get; set; }
}

Bước 5: Cấu hình chuỗi kết nối

Trong appsettings.json

{
  "ConnectionStrings": {
    "KyGuiConnection": "Server=.;Database=kygui;Trusted_Connection=True;TrustServerCertificate=True;"
  }
}

Trong Program.cs hoặc Startup.cs

builder.Services.AddDbContext<KyGuiDbContext>(options =>
   options.UseSqlServer(builder.Configuration.GetConnectionString("KyGuiConnection")

Bước 6: Tạo Migration & Database

Chạy lệnh sau trong terminal:

dotnet ef migrations add InitialCreate
dotnet ef database update

Bước 7: Kiểm tra

Bạn có thể inject KyGuiDbContext vào các Controller hoặc DAO để truy xuất dữ liệu như:

private readonly KyGuiDbContext _context;

public DeliveriesController(KyGuiDbContext context)
{
    _context = context;
}

Bước 8: Tạo thêm các Entity khác:

Trong tệp: KyGuiDbContext.cs thêm các lệnh sau

// === ENTITY ===
public class OnlinePayment
{
    public int Id { get; set; }
    public int OrderId { get; set; }
    public Order Order { get; set; }
    public string PaymentMethod { get; set; }
    public string TransactionId { get; set; }
    public string PaymentStatus { get; set; }
    public decimal PaidAmount { get; set; }
    public DateTime PaymentTime { get; set; }
}

public class Order
{
    public int Id { get; set; }
    public string OrderCode { get; set; }
    public int CustomerId { get; set; }
    public Customer Customer { get; set; }
    public DateTime OrderDate { get; set; }
    public string OrderStatus { get; set; }
    public ICollection<Product> Products { get; set; }
    public ICollection<OnlinePayment> Payments { get; set; }
    public ICollection<Delivery> Deliveries { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; } // áo, váy, giày, dép...
    public string Size { get; set; }
    public string Color { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
    public string ImageUrl { get; set; }
    public bool IsConsignment { get; set; } // true nếu là ký gửi
    public ICollection<Order> Orders { get; set; }
}

public class Customer
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
    public string Address { get; set; }
    public string PasswordHash { get; set; }
    public ICollection<Order> Orders { get; set; }
}

public class Shipper
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Phone { get; set; }
    public string VehicleNumber { get; set; }
    public ICollection<Delivery> Deliveries { get; set; }
}

// === DB CONTEXT ===
public class KyGuiDbContext : DbContext
{
    public KyGuiDbContext(DbContextOptions<KyGuiDbContext> options) : base(options) {}

    public DbSet<Delivery> Deliveries { get; set; }
    public DbSet<OnlinePayment> OnlinePayments { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Shipper> Shippers { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        // Cấu hình mối quan hệ nếu cần
    }
}

 

Tags: