






Preview text:
lOMoARcPSD| 61814229 Week78 Context CakeDbContext.cs using CakeMngt.Models; using
Microsoft.EntityFrameworkCore; namespace CakeMngt.Context {
public class CakeDbContext : DbContext {
public DbSet Cakes { get; set; } public DbSet Agents { get; set; }
protected override void OnCon 昀椀 guring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.UseSqlServer("Data Source=HIEN\\SQLEXPRESS;" +
"initial catalog=CakeDb; Trusted_Connection = True" +
"MultipleActiveResultSets=True"); } lOMoARcPSD| 61814229 } } Models Cake.cs using System;
using System.ComponentModel.DataAnnotations; namespace CakeMngt.Models { public class Cake { [Key]
[MaxLength(6)] public string CakeCode
{ get; set; } public string CakeName { get; set; }
public uint CakePrice { get; set; } public uint CakeWeigh { get; set; }
public DateTime ExpiredDate { get; set; } } } Reposistory ICake.cs using CakeMngt.Models; using System.Collections.Generic;
namespace CakeMngt.Reposistory { public interface ICake {
Cake GetCakeByCakeCode(string code);
List GetAllCake(); void AddCake(Cake cake);
void UpdateCake(Cake cake); void DeleteCake(Cake cakecode); } } CakeRepo.cs using CakeMngt.Context; using CakeMngt.Models; using
System.Collections.Generic; using System.Linq;
namespace CakeMngt.Reposistory {
public class CakeRepo : ICake { static CakeDbContext db; public CakeRepo() { db = new CakeDbContext(); } lOMoARcPSD| 61814229
public void AddCake(Cake objcake) {
db.Cakes.Add(objcake); db.SaveChanges();
// throw new NotImplementedException(); }
public void DeleteCake(Cake objcakedelete) {
db.Cakes.Remove(objcakedelete); db.SaveChanges();
// throw new NotImplementedException(); } public List GetAllCake() {
IEnumerable sql = from objs in db.Cakes
orderby objs.CakeName select objs; return sql.ToList();
// throw new NotImplementedException(); }
public Cake GetCakeByCakeCode(string code) {
Cake obj = (from a in db.Cakes
where a.CakeCode == code select
a).FirstOrDefault(); return obj;
// throw new NotImplementedException(); }
public void UpdateCake(Cake cake) {
Cake temp = (from a in db.Cakes where
cake.CakeCode == cake.CakeCode select a).FirstOrDefault();
temp.CakeName = cake.CakeName; temp.CakePrice =
cake.CakePrice; temp.CakeWeigh = cake.CakeWeigh;
temp.ExpiredDate = cake.ExpiredDate; db.SaveChanges();
// throw new NotImplementedException(); } } } Name Text/Value Type of control txtCakeCode TextBox txtCakeName TextBox numCakePrice 50000 NumericUpDown numCakeWeigh 100 NumericUpDown dtpExpiredDate currentDate (default) DateTimePicker cmdAdd Thêm Button cmdDelete Xóa Button cmdUpdate Sửa Button cmdSave Lưu Button cmdSkip Bỏ qua Button lOMoARcPSD| 61814229 picImageCake PictureBox dgvCakeList DataGridView GroupBox lOMoARcPSD| 61814229 Bai Thi Thu Context ProductContext.cs using ThiThu.Model;
using Microsoft.EntityFrameworkCore; namespace ThiThu.Context {
public class ProductContext : DbContext {
public DbSet Products { get; set; }
// end of declaration of table list
// Provide con 昀椀 g information relevant to ProductDB // table Products
protected override void OnCon 昀椀 guring(DbContextOptionsBuilder optionsBuilder) { //Trusted_Connection = True
//optionsBuilder.UseSqlServer("Data Source=LEGION\\SQLEXPRESS;initial catalog=CakeManagement; user
id=****;password=***;MultipleActiveResultSets=True");
optionsBuilder.UseSqlServer("Data Source=LEGION\\SQLEXPRESS;initial catalog=Product; Trusted_Connection =
True;MultipleActiveResultSets=True"); } } } Model Product.cs using System;
using System.ComponentModel.DataAnnotations; lOMoARcPSD| 61814229 namespace ThiThu.Model { public class Product { [Key]
public string prdCode { get; set; } public string
prdName { get; set; } public DateTime prdExpired
{ get; set; } public string prdProducer { get; set; }
public uint prdPrice { get; set; } } } Process IProduct.cs
using System.Collections.Generic; using ThiThu.Model; namespace ThiThu.Process { public interface IProduct {
int Add(Product product); int Update(Product
product, string code); int Delete(string code);
Product GetProduct(string code); List GetAllProduct(); } } ProductProcess.cs using System; using
System.Collections.Generic; using
System.Linq; using ThiThu.Model; using ThiThu.Context; namespace ThiThu.Process {
public class ProductProcess:IProduct {
public static ProductContext db; public ProductProcess() { }
public int Add(Product product) {
//step1: new ProductContext(); để thực hiện connect to db
//Insert vào table Products db = new ProductContext(); db.Products.Add(product); db.SaveChanges(); return 1; } lOMoARcPSD| 61814229
public int Delete(string code) {
//step1: new ProductContext(); để thực hiện connect to db
//Tìm kiếm sản phẩm trong db với code cho trước
// nêu tìm thấy, thực hiện Remove để delete and SaveChanges db = new ProductContext();
Product product = (from p in db.Products where p.prdCode == code select p).FirstOrDefault(); if (product != null) { db.Remove(product); db.SaveChanges(); return 1; } return 0; } public List GetAllProduct()
{ //step1: new ProductContext(); để thực hiện connect to db
//thực hiện truy vấn từ db.Products //ép
thành List để trả về db = new ProductContext();
IEnumerable allprd = from p in db.Products select p; return allprd.ToList(); }
public Product GetProduct(string code) {
throw new NotImplementedException(); }
public int Update(Product product, string code) { //kết nối csdl //lấy mã sản phẩm ra
// tìm kiếm trong db sản phẩm đó, // thay đổi dữ liệu // cập nhật db = new ProductContext();
Product prd = (from p in db.Products where p.prdCode == code select p).FirstOrDefault(); if (prd != null) {
prd.prdName = product.prdName; prd.prdExpired =
product.prdExpired; prd.prdProducer = product.prdProducer;
prd.prdPrice = product.prdPrice; // db.Remove(product); db.SaveChanges(); return 1; } return 0; } } }