private void addHistory_Click(object sender, EventArgs e) { var now = DateTime.Now.Date; var nextDay = now.AddDays(1); var topic = (SelectionData)selectTopic.SelectedItem; using (var db = new DuoContext()) { History his; if (!db.Histories.Any(x => x.CreatedDate >= now && x.CreatedDate < nextDay)) { his = new History() { CreatedDate = now }; db.Histories.Add(his); db.SaveChanges(); var start = startDate.Value; var end = endDate.Value; Reload(start, end); } else { his = db.Histories.FirstOrDefault(x => x.CreatedDate >= now && x.CreatedDate < nextDay && !x.HistoryDetails.Any(y => y.TopicId == topic.Id)); } if (his == null) { MessageBox.Show("Chủ đề đã kiểm tra và thêm lịch sử", "Trùng lập dữ liệu", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var hisDetail = new HistoryDetail() { HistoryId = his.Id, TopicId = topic.Id, DataTest = new byte[0] }; db.HistoryDetails.Add(hisDetail); db.SaveChanges(); listTopic.DataSource = null; var hisDetails = db.HistoryDetails.AsNoTracking() .Where(x => x.MyHistory.Id == his.Id) .OrderBy(x => x.Id) .Select(x => new { Id = x.Id, Title = x.MyTopic.Title }).ToList(); totalTopic.Text = hisDetails.Count.ToString(); listTopic.DataSource = hisDetails; listTopic.DisplayMember = "Title"; var topics = (List <SelectionData>)selectTopic.DataSource; var index = topics.FindIndex(x => x.Id == topic.Id); topics.RemoveAt(index); selectTopic.DataSource = null; selectTopic.DataSource = topics; selectTopic.DisplayMember = "Name"; } this.Refresh(); }
private void saveChangeTopic_Click(object sender, EventArgs e) { if (_topic == null && !string.IsNullOrWhiteSpace(topicTitle.Text)) { var hashData = HashService.ComputeSha256Hash(topicTitle.Text); using (var db = new DuoContext()) { if (db.Topics.AsNoTracking().Any(x => !x.IsDeleted && x.HashCompare.Equals(hashData))) { MessageBox.Show("Tiêu đề của chủ đề đã tồn tại trong Database", "Cảnh báo trùng lập dữ liệu" , MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } var count = db.Topics.AsNoTracking().Count(); var topic = new Topic() { Title = topicTitle.Text, HashCompare = hashData, IsDeleted = false, IsHide = isHide.Checked, Sort = count + 1 }; db.Topics.Add(topic); db.SaveChanges(); } LoadData(); return; } if (_topic != null && !string.IsNullOrWhiteSpace(topicTitle.Text)) { using (var db = new DuoContext()) { _topic.Title = topicTitle.Text; _topic.HashCompare = HashService.ComputeSha256Hash(topicTitle.Text); _topic.IsHide = isHide.Checked; var chk = db.Update <Topic>(_topic); } LoadData(); return; } MessageBox.Show("Vui lòng chọn một chủ đề hay nhập thông tin chủ đề", "Dữ liệu chưa có" , MessageBoxButtons.OK, MessageBoxIcon.Information); }
public static bool CreateOrCheckHistory() { var now = DateTime.Now; try { using (var db = new DuoContext()) { var chk = db.Histories.AsNoTracking().Any(x => x.CreatedDate.Date == now.Date); if (chk) { return(true); } var his = new History() { CreatedDate = now }; db.Histories.Add(his); db.SaveChanges(); return(true); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Function " + typeof(HistoryService).Name); return(false); } }