public void AddCustomer(Customer customerToAdd)
        {
            if (customerToAdd == null)
            {
                throw new ArgumentNullException(nameof(customerToAdd));
            }

            _context.Add(customerToAdd);
        }
Beispiel #2
0
        public async Task <IActionResult> Create([Bind("Id,Name,Phone,LastPurchase")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                _context.Add(customer);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(customer));
        }
Beispiel #3
0
        public async Task ResetData()
        {
            var notes = await _dbContext.Notes.ToListAsync();

            _dbContext.RemoveRange(notes);

            var customers = await _dbContext.Customers.ToListAsync();

            _dbContext.RemoveRange(customers);

            await _dbContext.SaveChangesAsync();

            for (int i = 1; i <= 10; i++)
            {
                var newCustomer = new Customer
                {
                    FirstName = $"First Name {i}",
                    LastName  = $"Last Name {i}",
                    City      = $"City {i}",
                    Country   = $"Country {i}",
                    Street    = $"Street {i}",
                    Status    = CustomerStatus.Current
                };

                for (int j = 1; j <= 3; j++)
                {
                    var newNote = new Note
                    {
                        Content = $"Content {j}"
                    };

                    newCustomer.Notes.Add(newNote);
                }

                _dbContext.Add(newCustomer);
            }

            await _dbContext.SaveChangesAsync();
        }
Beispiel #4
0
    public static void Interceptors_get_the_source_of_the_command()
    {
        Console.WriteLine($">>>> Sample: {nameof(Interceptors_get_the_source_of_the_command)}");
        Console.WriteLine();

        using var context = new CustomersContext();

        context.Database.EnsureDeleted();
        context.Database.EnsureCreated();

        context.Add(
            new Customer
        {
            Name = "Sam Vimes"
        });

        context.SaveChanges();

        context.ChangeTracker.Clear();

        var customers = context.Customers.ToList();

        Console.WriteLine();
    }