protected override async Task AddAsync() { this._data = new iccard(); if (this._dataModel != null && this._data != null) { // TODO: transcation 機制 // 現有方式在資料有錯的時候無法復原 ViewModelToModel(); using (var ts = new TransactionScope()) { try { await this._dataModel.InsertAsync(this._data); foreach (var deviceAddress in this.RelatedDevices) { icmap m = new icmap { C_icno = this._data.C_icno, C_entrancedoor = deviceAddress as string }; await this._securityCardDeviceDataService.InsertAsync(m); } ts.Complete(); } catch (Exception) { } } RefreshCommand.Execute(null); } }
protected override async Task UpdateAsync() { if (this._dataModel != null && this._data != null) { ViewModelToModel(); using (var ts = new TransactionScope()) { try { await this._dataModel.UpdateAsync(this._data); await this._securityCardDeviceDataService.DeleteAsync(m => m.C_icno == this._data.C_icno); foreach (var deviceAddress in this.RelatedDevices) { icmap m = new icmap { C_icno = this._data.C_icno, C_entrancedoor = deviceAddress as string }; await this._securityCardDeviceDataService.InsertAsync(m); } ts.Complete(); } catch (Exception) { } } RefreshCommand.Execute(null); } }
private void BtnSave_Click(object sender, EventArgs e) { if (m_Card != null)// Moding mode { if ((textBoxRo.Text != "") && (textBoxResidentName.Text != "")) { //using (ICMDBContext db = new ICMDBContext()) { int lastMap = 0; try { lastMap = db.Icmaps.OrderByDescending(p => p.C_id).Select(r => r.C_id).First(); } catch { } var card = (from c in db.Iccards where c.C_icno == m_Card.C_icno select c).FirstOrDefault(); if (card != null) { db.Iccards.Remove(card); db.SaveChanges(); card.C_icno = textBoxICCard.Text; card.C_roomid = textBoxRo.Text; card.C_available = ComboBoxOnUse.SelectedIndex; card.C_ictype = ComboBoxIsResident.SelectedIndex; card.C_username = textBoxResidentName.Text; if (checkBoxStartDate.Checked) { card.C_uptime = dateTimePickerStart.Value; } else { card.C_uptime = null; } if (checkBoxEndDate.Checked) { card.C_downtime = dateTimePickerEnd.Value; } else { card.C_downtime = null; } card.C_icid = id; db.Iccards.Add(card); db.SaveChanges(); } // delete map var map = from foo in db.Icmaps where foo.C_icno == card.C_icno select foo; db.Icmaps.RemoveRange(map); db.SaveChanges(); // del end foreach (ListViewItem tmp in listViewRelate.Items) { string ro = tmp.SubItems[0].Text; icmap MapSave = new icmap { C_icno = textBoxICCard.Text, C_entrancedoor = DevicesAddressConverter.ChStrToRo(ro), C_id = lastMap + 1 }; db.Icmaps.Add(MapSave); } db.SaveChanges(); this.Close(); } } else { MessageBox.Show(strings.CardRoNameCannotBeNull);//"卡号、房号、姓名不得为空!"); } } else// Adding mode { if (Check()) { //using (ICMDBContext db = new ICMDBContext()) { int last = 0; int lastMap = 0; try { last = db.Iccards.OrderByDescending(p => p.C_icid).Select(r => r.C_icid).First(); } catch { } try { lastMap = db.Icmaps.OrderByDescending(p => p.C_id).Select(r => r.C_id).First(); } catch {} Iccard CardSave = new Iccard { C_icno = textBoxICCard.Text, C_roomid = textBoxRo.Text, C_available = ComboBoxOnUse.SelectedIndex, C_ictype = ComboBoxIsResident.SelectedIndex, C_username = textBoxResidentName.Text, C_uptime = dateTimePickerStart.Value, C_downtime = dateTimePickerEnd.Value, C_icid = last + 1 }; foreach (ListViewItem tmp in listViewRelate.Items) { string ro = tmp.SubItems[0].Text; icmap MapSave = new icmap { C_icno = textBoxICCard.Text, C_entrancedoor = DevicesAddressConverter.ChStrToRo(ro), C_id = lastMap + 1 }; db.Icmaps.Add(MapSave); } db.Iccards.Add(CardSave); db.SaveChanges(); this.Close(); } } } }