public void InsertSimpLetters() { using (var context = new UNSModel()) { var y = new SimplifiedLetter() { DocumentName = "sfsdfsdf", OutgoingDate = DateTime.Now, Recipient = "ssdfsdf" }; context.Set <SimplifiedLetter>().Add(y); context.SaveChanges(); context.Set <SimplifiedLetter>().Remove(y); context.SaveChanges(); } }
internal void CreateLetters() { var currentBookName = Path.GetFileNameWithoutExtension(Globals.ThisAddIn.Application.ActiveWorkbook.Name); try { Excel.Range firm = Globals.ThisAddIn.Application.Selection as Excel.Range; using (var context = new UNSModel()) { var fullselected = (from row in ReestrSheet() join unsrow in context.IntegraDUStages on row.UNIU equals unsrow.UNIU where row.HouseOwner != null && row.HouseOwner.ToString() == firm.Value2 && row.LetterOutData == null && row.LetterOutNumber == null orderby row.AddressObject, row.AddressHouse select new { row, unsrow }).ToList(); var unsselected = fullselected.Select(s => s.unsrow); var selected = fullselected.Select(s => s.row); if (selected.Any()) { var currentDateLetterCount = context.SimplifiedLetters.Where(w => w.OutgoingDate == DateTime.Today).Count(); currentDateLetterCount++; var selectedFirst = selected.FirstOrDefault(); var newLeller = new SimplifiedLetter() { OutgoingNumber = string.Join("/", DateTime.Now.ToString("yyyyMMdd"), currentDateLetterCount.ToString()), OutgoingDate = DateTime.Now.Date, Recipient = selectedFirst.HouseOwner?.ToString(), RecipientDirectorName = selectedFirst.Director?.ToString(), RecipientDirectorPosition = selectedFirst.DirectorPosition?.ToString(), IntegraDUStages = unsselected.ToList() }; Excel.Workbook newWB = Globals.ThisAddIn.Application.Workbooks .Add(LettersTemplate.FullName); for (int n = 0; n <= selected.Count() - 1; n++) { Excel.Range destRow = newWB.Sheets[1].Cells.Rows[n + 2]; destRow.Cells[1, 1].Value = n + 1; destRow.Cells[1, 2].Value = selected.ElementAt(n).AddressObject; destRow.Cells[1, 3].Value = selected.ElementAt(n).AddressHouse; destRow.Cells[1, 4].Value = selected.ElementAt(n).UNOM; destRow.Cells[1, 5].Value = selected.ElementAt(n).UNIU; if (selected.ElementAt(n).LetterOutNumber == null) { selected.ElementAt(n).LetterOutNumber = newLeller.OutgoingNumber; } if (selected.ElementAt(n).LetterOutData == null) { selected.ElementAt(n).LetterOutData = newLeller.OutgoingDate; } } var newpath = Path.Combine(rootletters.FullName, currentBookName, "На отправку", firm.Value2.Replace(@"\\", ":").Replace(@"""", ""));//+ ".xlsx" newWB.SaveAs(newpath, Excel.XlFileFormat.xlOpenXMLWorkbook); newWB.Close(); var oper = new QueryToHouseOwner_Word_Operator(); oper.Create(newpath, newLeller.Recipient, newLeller.RecipientDirectorPosition, newLeller.RecipientDirectorName, newLeller.OutgoingNumber, newLeller.OutgoingDate ); context.SimplifiedLetters.Add(newLeller); context.SaveChanges(); } } } catch (Exception innere) { Logger.Logger.Error(innere.Message); } finally { EnableCalculations(true); } }