protected void btnDatabindRepository_Click(object sender, EventArgs e)
 {
     using (var repository = new CustomerRepository())
     {
         customersView.DataSource = repository.GetAll();
         customersView.DataBind();
     }
 }
        public void ConcurrencyTests()
        {
            //Get the same record but loaded from two separate contexts so the tracking won't overlap.

            ICustomerRepository repository = new CustomerRepository(_dbContext);
            var secondContext = new EntityContext();

            //add new customer to db.
            var newCustomer = CreateCustomer(repository);

            //load this customer from context 1
            var firstCustomer1 = repository.GetById(newCustomer.CustomerId);
            firstCustomer1.City = "newCity3";

            //load the same customer from context 2
            ICustomerRepository secondRepository = new CustomerRepository(secondContext);
            var firstCustomer2 = secondRepository.GetById(firstCustomer1.CustomerId);

            //Check the states
            Debug.WriteLine(_dbContext.Entry(newCustomer).State);
            Debug.WriteLine(_dbContext.Entry(firstCustomer1).State);
            Debug.WriteLine(secondContext.Entry(firstCustomer2).State);

            //Modify the second customer. We have the same TimeStamp as the original record
            //however the timestamp in the database would've been updated already.
            firstCustomer2.Address = "222 main st" + DateTime.Now.ToString();
            Debug.WriteLine(secondContext.Entry(firstCustomer2).State);

            //This should update that customer.
            repository.Update(firstCustomer1);
            repository.Save();

            //This should throw an exception.
            secondRepository.Update(firstCustomer2);
            secondRepository.Save();

            secondContext.Dispose();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var repository = new CustomerRepository())
            {
                //Load it
                var customers = repository.GetAll();

                //Map it to view model
                var customerViewModels = customers.Select(c => new CustomerViewModel
                {
                    CustomerId = c.CustomerId,
                    Address = c.Address,
                    City = c.City,
                    State = c.State,
                    FirstName = c.FirstName,
                    LastName = c.LastName
                }).ToArray();

                //Bind it. BAM
                GridView1.DataSource = customerViewModels;
                GridView1.DataBind();
            }
        }
 private IEnumerable<SelectListItem> GetAllCustomers()
 {
     using (CustomerRepository repository = new CustomerRepository())
     {
         return repository.GetAll().Select(x => new SelectListItem
         {
             Text = string.Format("{0} {1}", x.FirstName, x.LastName),
             Value = x.CustomerId.ToString()
         }).ToList();
     }
 }
        public void InsertAndLoadCustomer()
        {
            //Add to context just to show its state prior to save
            Customer customerNew = new Customer(_validator);
            customerNew.FirstName = "Mary";
            customerNew.LastName = "Doe";
            _dbContext.Customers.Attach(customerNew);
            //State is 'unchanged' no errors raised by simply adding it.
            Debug.WriteLine(_dbContext.Entry(customerNew).State);

            //Insert via repository
            ICustomerRepository repository = new CustomerRepository(_dbContext);
            //creates and saves it
            var customer = CreateCustomer(repository);
            //Get its state from the context - should be unchanged (as its saved)
            Debug.WriteLine(_dbContext.Entry(customer).State);
            //Saving again does nothing to the database (see profiler)
            repository.Update(customer);
            repository.Save();

            //update the first name.
            customer.FirstName = "John";
            //Get its state from the context
            Debug.WriteLine(_dbContext.Entry(customer).State);
            //saving again notice only the first name is updated.
            repository.Update(customer);
            repository.Save();

            Customer insertCustomer = new Customer(_validator);
            insertCustomer.FirstName = "Jane";
            insertCustomer.LastName = "Doe";
            insertCustomer.Address = "555 main st";
            insertCustomer.City = "Orlando";
            insertCustomer.Zip = "33400";
            insertCustomer.State = "FL";
            Debug.WriteLine(insertCustomer.CustomerId);
            _dbContext.Customers.Add(insertCustomer);
            Debug.WriteLine(_dbContext.Entry(insertCustomer).State);
            _dbContext.SaveChanges();
            //Note the primary key is automatically updated.
            Debug.WriteLine(insertCustomer.CustomerId);
            Debug.WriteLine(_dbContext.Entry(insertCustomer).State);
        }
        public void TestNoKey()
        {
            ICustomerRepository repository = new CustomerRepository(_dbContext);
            var bip = new Customer(_validator);
            bip.CustomerId = 10;
            bip.CustomerId = 20;
            var customer = CreateCustomer(repository);
            _dbContext.Customers.Attach(bip);
            bip.CustomerId = 30;
            Debug.WriteLine(_dbContext.Entry(bip).State);
            var newContext = new EntityContext();
            var repository2 = new CustomerRepository(newContext);
            Debug.WriteLine(newContext.Entry(customer).State);
            customer.Address = "555";
            Debug.WriteLine(newContext.Entry(customer).State);
            //newContext.Entry(customer).State = System.Data.EntityState.Modified;
            Debug.WriteLine(newContext.Entry(customer).State);

            newContext.Customers.Attach(customer);
            customer.CustomerId = 0;
            newContext.SaveChanges();

            repository2.Update(customer);
            repository2.Save();
            customer.Address = "556";
            Debug.WriteLine(newContext.Entry(customer).State);
        }
 public void TestEnumerableOutsideContext()
 {
     IEnumerable<Customer> customers;
     using (EntityContext context = new EntityContext())
     {
         var repository = new CustomerRepository(context);
         customers = repository.GetAllEnumerable();
     }
     //note the The operation cannot be completed because the DbContext has been disposed.
     Debug.WriteLine(string.Format("Customer count:{0}", customers.Count()));
 }
        public void TestCustomerLoading()
        {
            var customer = _dbContext.Customers.Where(o => o.CustomerId == 1);

            ICustomerRepository repository = new CustomerRepository(_dbContext);
            Debug.WriteLine(customer.Single().FirstName);

            var customers = repository.GetAll();
            //Debug.Assert(customers.Count == 0);
            Customer customer2 = new Customer(_validator) { FirstName = "Test", City = "bethlehem" };
            repository.Create(customer2);
            repository.Save();
        }
        public void TestAddCustomerMissingRequiredFields()
        {
            using (EntityContext context = new EntityContext())
            {
                Customer customer = new Customer(_validator);

                ICustomerRepository repository = new CustomerRepository(context);
                repository.Create(customer);
                repository.Save();
            }
        }
        public void ShowSmartFieldUpating()
        {
            ICustomerRepository repository = new CustomerRepository(_dbContext);
            var customers = repository.GetAll();
            Debug.Assert(customers.Count() == 0);

            Customer customer = new Customer(_validator) { FirstName = "Test", City = "bethlehem" };
            repository.Create(customer);
            repository.Save();
            customer.Address = "1 main st" + DateTime.Now.ToString();
            customer.City = customer.City;
            repository.Update(customer);
            repository.Save();
        }
        public void InsertAndUpdateCustomer()
        {
            ICustomerRepository repository = new CustomerRepository(_dbContext);
            var customer = CreateCustomer(repository);

            Debug.WriteLine(_dbContext.Entry(customer).State);
            customer.City = "City " + DateTime.Now.ToString();
            Debug.WriteLine(_dbContext.Entry(customer).State);

            repository.Update(customer);
            repository.Save();
            Debug.WriteLine(_dbContext.Entry(customer).State);
        }