Tin tức
Tạo cơ sở dữ liệu chuẩn seo cho website tin tức , asp.net core mvc8 và MySQL (Xampp)
Hướng dẫn code first tạo database chuẩn seo cho website tin tức dùng Migartion để ánh xạ dữ liệu
Xây dựng một hệ thống cơ sở dữ liệu chuẩn hóa và thân thiện với công cụ tìm kiếm, nhằm:
Tối ưu hóa URL bài viết, cải thiện tốc độ truy xuất dữ liệu. Hỗ trợ các thành phần SEO như Meta Title, Meta Description, Slug, Canonical URL.
Hỗ trợ phân loại bài viết theo thể loại, loại tin.
Đây là bảng sơ đồ quan hệ của website “tin tức”
Bảng | Vai trò |
---|---|
TheLoai | Danh mục chính (chính trị, thể thao, công nghệ…) |
LoaiTin | Danh mục con thuộc TheLoai |
BaiViet | Bài viết tin tức |
NguoiDung | Người đăng bài viết |
BinhLuan | Bình luận từ người dùng |
TraLoiBinhLuan | Trả lời bình luận |
Bắt đầu tạo các class ở Model để ánh xạ database code first
Tạo Model : TheLoai.cs
public class TheLoai
{
public int Id { get; set; }
public string TenTheLoai { get; set; }
public string Slug { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public virtual ICollection<LoaiTin> LoaiTins { get; set; }
public virtual ICollection<BaiViet> BaiViets { get; set; }
}
public class LoaiTin
{
public int Id { get; set; }
public string TenLoaiTin { get; set; }
public string Slug { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }
public int TheLoaiId { get; set; }
public TheLoai TheLoai { get; set; }
public virtual ICollection<BaiViet> BaiViets { get; set; }
}
Tạo Model : BaiViet.cs
public class BaiViet
{
public int Id { get; set; }
public string MetaTitle { get; set; }
public string MetaDescription { get; set; }}
public string Slug { get; set; }
public string NoiDung { get; set; }
public string HinhAnh { get; set; }
public DateTime NgayDang { get; set; } = DateTime.Now;
public int TheLoaiId { get; set; }
public TheLoai TheLoai { get; set; }
public int LoaiTinId { get; set; }
public LoaiTin LoaiTin { get; set; }
public int NguoiDungId { get; set; }
public NguoiDung NguoiDung { get; set; }
public virtual ICollection<BinhLuan> BinhLuans { get; set; }
}
Tạo Model : NguoiDung.cs
public class NguoiDung
{
public int Id { get; set; }
public string HoTen { get; set; }
public string Email { get; set; }
public string PasswordHash { get; set; }
public virtual ICollection<BaiViet> BaiViets { get; set; }
public virtual ICollection<BinhLuan> BinhLuans { get; set; }
public virtual ICollection<TraLoiBinhLuan> TraLoiBinhLuans { get; set; }
}
Tạo Model : BinhLuan.cs
public class BinhLuan
{
public int Id { get; set; }
public string NoiDung { get; set; }
public DateTime NgayDang { get; set; } = DateTime.Now;
public int BaiVietId { get; set; }
public BaiViet BaiViet { get; set; }
public int NguoiDungId { get; set; }
public NguoiDung NguoiDung { get; set; }
public ICollection<TraLoiBinhLuan> TraLoiBinhLuans { get; set; }
}
Tạo Model : TraLoiBinhLuan.cs
public class TraLoiBinhLuan
{
public int Id { get; set; }
public string NoiDung { get; set; }
public DateTime NgayDang { get; set; } = DateTime.Now;
public int BinhLuanId { get; set; }
public BinhLuan BinhLuan { get; set; }
public int NguoiDungId { get; set; }
public NguoiDung NguoiDung { get; set; }
}
Thiết lập DBContext
Tạo Model ApplicationDbContext.cs

Cuối cùng Migration CSDL
PM> add-migration KhoiTaoDuLieu
PM> update-database
Vào AdminMysql để kiểm tra CSDL đã tạo hoàn chỉnh hay chưa, nếu chưa thì có thể lỗi cấu hình sai, chưa cấu hình appseting.json, hoặc chưa đăng ký kêt nối mysql ở file Programs.cs. Chúc bạn thành công.