static void Main() { using (var db = new DalContext()) { AppDomain.CurrentDomain.SetData("DataDirectory", AppDomain.CurrentDomain.BaseDirectory); // Create and save a new equipment //Console.Write("Enter a name for a new Equipment: "); //var name = Console.ReadLine(); //var equipment = new Equipment { Name = name }; //db.Equipments.Add(equipment); //db.SaveChanges(); // Display all equipments from the database var query = from b in db.Equipments orderby b.Name select b; Console.WriteLine("All equipment in the database:"); foreach (var item in query) { Console.WriteLine(item.Name + "\t" + item.Type); } Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
private static int GetLastInvoiceId(DalContext context) { var invoices = context.Invoices; var maxId = invoices.Where(i => i.Id == invoices.Max(inv => inv.Id)).Select(i => i.Id).SingleOrDefault(); return maxId; }
public InvoiceFileDto ConfirmCart(CartDto cart) { InvoiceFileDto fileDto; using (var context = new DalContext()) { var invoice = SaveInvoice(cart, context); // if code wont call this DbSet, all lazy requests for InvoiceRow.Equipment property will return null var equipments = context.Equipments.ToList(); var fees = context.Fees.ToDictionary(f => f.Type, f => f); fileDto = PrepareInvoiceFile(invoice, fees); } return fileDto; }
public ICollection<EquipmentDto> GetInventoryList() { using (var context = new DalContext()) { var equipments = context.Equipments; var equipmentDtos = equipments.Select(eq => new EquipmentDto { Id = eq.Id, Name = eq.Name, Type = eq.Type.ToString() }).ToList(); return equipmentDtos; } }
private static Invoice SaveInvoice(CartDto cart, DalContext context) { var invoice = new Invoice(); invoice.Customer = cart.CustomerName; invoice.Date = DateTime.Now; invoice.Rows = cart.Rows.Select(cr => GetInvoiceRow(cr, invoice)).ToList(); var lastId = GetLastInvoiceId(context); invoice.Number = string.Format("#{0:D6}", lastId + 1); context.Invoices.Add(invoice); context.SaveChanges(); invoice = context.Invoices.Single(inv => inv.Id == invoice.Id); return invoice; }