Esempio n. 1
0
 public int Complete()
 {
     return(_context.SaveChanges());
 }
Esempio n. 2
0
        public void Seed(StoreContext context, ILoggerFactory loggerFactory)
        {
            try
            {
                if (!context.Customers.Any())
                {
                    var customersData = File.ReadAllText("../Infrastructure/Data/SeedData/customers.json");
                    var customers     = JsonSerializer.Deserialize <List <Customer> >(customersData);

                    foreach (var cust in customers)
                    {
                        context.Customers.Add(cust);
                    }
                }

                if (!context.Orders.Any())
                {
                    var ordersData = File.ReadAllText("../Infrastructure/Data/SeedData/orders.json");
                    var orders     = JsonSerializer.Deserialize <List <OrderImportDto> >(ordersData);

                    foreach (var order in orders)
                    {
                        var orderToImport = new Order
                        {
                            TotalPrice = order.TotalPrice,
                            Placed     = DateTime.ParseExact(order.Placed, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture),
                            CustomerId = order.CustomerId
                        };

                        orderToImport.Completed = string.IsNullOrWhiteSpace(order.Completed) ? (DateTime?)null : DateTime.ParseExact(order.Completed, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
                        context.Orders.Add(orderToImport);
                    }
                }

                if (!context.Servers.Any())
                {
                    var serversData = File.ReadAllText("../Infrastructure/Data/SeedData/servers.json");
                    var servers     = JsonSerializer.Deserialize <List <ServerImportDto> >(serversData);

                    foreach (var serverToImport in servers)
                    {
                        var server = new Server
                        {
                            Name     = serverToImport.Name,
                            IsOnline = serverToImport.IsOnline
                        };

                        server.LastDownDate = string.IsNullOrWhiteSpace(serverToImport.LastDownDate) ?
                                              (DateTime?)null : DateTime.ParseExact(serverToImport.LastDownDate, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);

                        context.Servers.Add(server);
                    }
                }

                context.SaveChanges();
            }
            catch (Exception e)
            {
                var logger = loggerFactory.CreateLogger <StoreContext>();
                logger.LogError(e.StackTrace);
            }
        }
Esempio n. 3
0
 public void AddProduct(T entity)
 {
     _context.Set <T>().Add(entity);
     _context.SaveChanges();
 }