public static async void Import(string[] args) { var factory = new OrderbookContextFactory(); using var context = factory.CreateDbContext(args); var fileLinesCustomer = await File.ReadAllLinesAsync(args[1]); string[] line; for (int i = 1; i < fileLinesCustomer.Count(); i++) { line = fileLinesCustomer[i].Split("\t"); var customer = new Customer { Name = line[0], CreditLimit = decimal.Parse(line[1]) }; context.Customers.Add(customer); } await context.SaveChangesAsync(); fileLinesCustomer = await File.ReadAllLinesAsync(args[2]); for (int i = 1; i < fileLinesCustomer.Count(); i++) { line = fileLinesCustomer[i].Split("\t"); var customer = context.Customers.Where(c => c.Name == line[0]).ToArray(); var order = new Order { CustomerId = customer[0].Id, OrderDate = DateTime.Parse(line[1]), OrderValue = decimal.Parse(line[2]) }; context.Orders.Add(order); } await context.SaveChangesAsync(); }
public static async void Clean(string[] args) { var factory = new OrderbookContextFactory(); using var context = factory.CreateDbContext(args); context.Customers.RemoveRange(context.Customers); context.Orders.RemoveRange(context.Orders); await context.Database.ExecuteSqlRawAsync("DBCC CHECKIDENT('Orders', RESEED, 0)"); await context.Database.ExecuteSqlRawAsync("DBCC CHECKIDENT('Customers', RESEED, 0)"); await context.SaveChangesAsync(); }
public static async void Check(string[] args) { var factory = new OrderbookContextFactory(); using var context = factory.CreateDbContext(args); var orders = await context.Orders.ToListAsync(); foreach (var customer in context.Customers) { var sumOfOrderValue = orders.Where(x => customer.Id == x.CustomerId).Sum(x => x.OrderValue); if (sumOfOrderValue > customer.CreditLimit) { Console.WriteLine($"Name: {customer.Name}:\tLimit: {customer.CreditLimit} OrderValue: {sumOfOrderValue}"); } } }