public ProvincialDepartment Put([FromForm] ProvincialDepartmentRequest request, long id) { Console.WriteLine("department 1 :" + id + "///" + request.Name); var provincialdepartmentdata = _context.ProvincialDepartment.Find(id); provincialdepartmentdata.Name = request.Name; _context.Entry(provincialdepartmentdata).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.SaveChanges(); // <! -- ลบข้อมูล --> var ProvincialDepartmentProvince = _context.ProvincialDepartmentProvince .Where(m => m.ProvincialDepartmentID == id); _context.ProvincialDepartmentProvince.RemoveRange(ProvincialDepartmentProvince); _context.SaveChanges(); // <! -- END ลบข้อมูล --> foreach (var item in request.Province) { Console.WriteLine("test 2 :"); var provincedata = new ProvincialDepartmentProvince { ProvincialDepartmentID = id, ProvinceId = item }; _context.ProvincialDepartmentProvince.Add(provincedata); _context.SaveChanges(); } return(provincialdepartmentdata); }
public ProvincialDepartment Post([FromForm] ProvincialDepartmentRequest request) { Console.WriteLine("test 1 :" + request.DepartmentId); var date = DateTime.Now; var provincialdepartmentdata = new ProvincialDepartment { DepartmentId = request.DepartmentId, Name = request.Name, CreatedAt = date }; _context.ProvincialDepartment.Add(provincialdepartmentdata); _context.SaveChanges(); foreach (var item in request.Province) { Console.WriteLine("test 2 :"); var provincedata = new ProvincialDepartmentProvince { ProvincialDepartmentID = provincialdepartmentdata.Id, ProvinceId = item }; _context.ProvincialDepartmentProvince.Add(provincedata); _context.SaveChanges(); } Console.WriteLine("test 3 :"); return(provincialdepartmentdata); }