public void Initialize()
 {
     _dbContext = new EntityContext();
     _validator= new CustomerValidator();
     //AlwaysRecreateDatabase
     //CreateDatabaseOnlyIfNotExists
     //RecreateDatabaseIfModelChanges
     Database.SetInitializer(new CreateDatabaseIfNotExists<EntityContext>());
 }
 protected void btnDatabindOutsideOfContext_Click(object sender, EventArgs e)
 {
     IEnumerable<Customer> customers;
     using (var context = new EntityContext())
     {
         customers = context.Customers;//.Where(o=>o.FirstName.Contains("a"));
     }
     //Note intentional error
     customersView.DataSource = customers.ToArray();
     customersView.DataBind();
 }
        protected void btnDataBindInContext_Click(object sender, EventArgs e)
        {
            using (var context = new EntityContext())
            {
                //dont
                //customersView.DataSource = from o in context.Customers select o;
                //do:

                customersView.DataSource = context.Customers.ToArray();
                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();
        }
        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 TestEagerLoading()
 {
     using (EntityContext context = new EntityContext())
     {
         var repository = new OrderRepository(context);
         var orders = repository.GetAll();
         Debug.WriteLine(string.Format("Order count:{0}", orders.Count()));
         var firstOrder = orders.First();
         //firstOr
         //context.SaveChanges();
     }
 }
        public void TestAddCustomerMissingRequiredFields()
        {
            using (EntityContext context = new EntityContext())
            {
                Customer customer = new Customer(_validator);

                ICustomerRepository repository = new CustomerRepository(context);
                repository.Create(customer);
                repository.Save();
            }
        }
        //For next stage of unit of work implementation using IContext
        //public UnitOfWork(IContext context)
        //{
        //    _context = context;
        //}

        public UnitOfWork(EntityContext context)
        {
            _context = context;
        }