public async Task <IActionResult> Create([Bind("Make,Color,PetName,Id,Timestamp")] Inventory inventory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(inventory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(inventory));
        }
        public async Task <IActionResult> Create([Bind("FirstName,LastName,Id,Timestamp")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Exemple #3
0
        public async Task <IActionResult> Create([Bind("CustomerId,CarId,Id,Timestamp")] Order order)
        {
            if (ModelState.IsValid)
            {
                _context.Add(order);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CarId"]      = new SelectList(_context.Cars, "Id", "Id", order.CarId);
            ViewData["CustomerId"] = new SelectList(_context.Customers, "Id", "Id", order.CustomerId);
            return(View(order));
        }
        public static void InitializeData(AutoLotContext context)
        {
            var customers = new List <Customer>
            {
                new Customer {
                    FirstName = "Dave", LastName = "Brener"
                },
                new Customer {
                    FirstName = "Matt", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Steve", LastName = "Hagen"
                },
                new Customer {
                    FirstName = "Pat", LastName = "Walton"
                },
                new Customer {
                    FirstName = "Bad", LastName = "Customer"
                }
            };

            customers.ForEach(x => context.Customers.Add(x));
            context.SaveChanges();

            var cars = new List <Inventory>
            {
                new Inventory {
                    Make = "VW", Color = "Black", PetName = "Zippy"
                },
                new Inventory {
                    Make = "Ford", Color = "Rust", PetName = "Rusty"
                },
                new Inventory {
                    Make = "Saab", Color = "Black", PetName = "Mel"
                },
                new Inventory {
                    Make = "BMW", Color = "Green", PetName = "Hank"
                }
            };

            context.Cars.AddRange(cars);
            context.SaveChanges();

            var orders = new List <Order>
            {
                new Order {
                    Car = cars[0], Customer = customers[0]
                },
                new Order {
                    Car = cars[1], Customer = customers[1]
                },
                new Order {
                    Car = cars[2], Customer = customers[2]
                },
                new Order {
                    Car = cars[3], Customer = customers[3]
                }
            };

            orders.ForEach(x => context.Add(x));
            context.SaveChanges();

            context.CreditRisks.Add(new CreditRisk
            {
                Id        = customers[4].Id,
                FirstName = customers[4].FirstName,
                LastName  = customers[4].LastName
            });

            context.Database.OpenConnection();
            try
            {
                //        var tName = context.GetTableName(typeof(CreditRisk));
                // We need dbcontext to access the models
                var models = context.Model;

                // Get all the entity types information
                var entityTypes = models.GetEntityTypes();

                // T is Name of class
                var entityTypeOfT = entityTypes.First(t => t.ClrType == typeof(CreditRisk));

                var tableNameAnnotation = entityTypeOfT.GetAnnotation("Relational:TableName");
                var tableName           = tableNameAnnotation.Value.ToString();

                var command = $"SET IDENTITY_INSERT dbo.{tableName} ON;";

                context.Database.ExecuteSqlCommand(command);
                context.SaveChanges();
                command = $"SET IDENTITY_INSERT dbo.CreditRisks OFF;";
                context.Database.ExecuteSqlCommand(command);
            }
            finally
            {
                context.Database.CloseConnection();
            }
        }