private async void BtnNew_Click(object sender, EventArgs e) { var page = new Homepage { CreateDate = DateTime.Now, Deleted = false, Description = TbDescription.Text, Link = TbLink.Text }; _pages.Add(page); TvPages.AddObject(page); if (_isOpenedByEditor) { return; } var db = new DataBase(); switch (_index) { case 1: var nodeToOrga = new HomepageToOrganization { CreateDate = DateTime.Now, Deleted = false, HomepageId = page.Id, OrganizationId = _id }; db.HomepageToOrganizationNode.Add(nodeToOrga); break; case 2: var nodeToGroup = new HomepageToGroup { CreateDate = DateTime.Now, Deleted = false, HomepageId = page.Id, GroupId = _id }; db.HomepageToGroupNode.Add(nodeToGroup); break; case 3: var nodeToMember = new HomepageToMember { CreateDate = DateTime.Now, Deleted = false, HomepageId = page.Id, MemberId = _id }; db.HomepageToMemberNode.Add(nodeToMember); break; } db.Homepages.Add(page); await db.SaveChangesAsync(); }
private async void BtnSave_Click(object sender, EventArgs e) { _group.Picture = ImageByteConverter.ImageToBytes(PbImage.Image); _group.Description = TbDescription.Text; _group.Name = TbName.Text; _group.CreateDate = DateTime.Now; var db = new DataBase(); if (_group.Id == 0) { db.Groups.Add(_group); var node = new GroupToOrganization { CreateDate = DateTime.Now, Group = _group, Deleted = false, OrganizationId = ((Organization)CbOrganizations.SelectedItem).Id }; db.GroupToOrganizationNode.Add(node); } else { db.Entry(_group).State = EntityState.Modified; var oldGroupNode = await db.GroupToOrganizationNode .FirstOrDefaultAsync(d => !d.Deleted && d.GroupId == _group.Id && d.OrganizationId == ((Organization)CbOrganizations.SelectedItem).Id); if (oldGroupNode != null) { oldGroupNode.Deleted = CheckOldLink.Checked; var newGroupNode = new GroupToOrganization { CreateDate = DateTime.Now, GroupId = _group.Id, Deleted = false, OrganizationId = ((Organization)CbOrganizations.SelectedItem).Id }; db.GroupToOrganizationNode.Add(newGroupNode); } } if (_gotEmails) { foreach (var email in _emails) { if (email.GroupNode.Any(d => !d.Deleted && d.GroupId == _group.Id)) { continue; } var emailToGroupNode = new EmailToGroup { EmailId = email.Id, CreateDate = DateTime.Now, Deleted = false, GroupId = _group.Id }; db.EmailToGroupNode.Add(emailToGroupNode); } db.Emails.AddRange(_emails); } if (_gotHomepage) { foreach (var homepage in _homepages) { if (homepage.GroupNode.Any(d => !d.Deleted && d.GroupId == _group.Id)) { continue; } var homepageToGroupNode = new HomepageToGroup { HomepageId = homepage.Id, CreateDate = DateTime.Now, Deleted = false, GroupId = _group.Id }; db.HomepageToGroupNode.Add(homepageToGroupNode); } db.Homepages.AddRange(_homepages); } if (_gotPhone) { foreach (var number in _phoneNumbers) { if (number.GroupNode.Any(d => !d.Deleted && d.GroupId == _group.Id)) { continue; } var phoneToGroupNode = new PhoneToGroup { PhoneNumberId = number.Id, CreateDate = DateTime.Now, Deleted = false, GroupId = _group.Id }; db.PhoneToGroupNode.Add(phoneToGroupNode); } db.PhoneNumbers.AddRange(_phoneNumbers); } await db.SaveChangesAsync(); Close(); }