public void InsertCustomer(DomainModels.Customer customer)
        {
            using (RetailEntities context = new RetailEntities())
            {
                Customer entity = Mapper.Map <DomainModels.Customer, Customer>(customer);
                context.Customers.AddObject(entity);
                context.SaveChanges();

                customer.Id = entity.Id;
            }
        }
        public void UpdateCustomer(DomainModels.Customer customer)
        {
            using (RetailEntities context = new RetailEntities())
            {
                Customer entity = context.Customers.AsQueryable().Single(c => c.Id == customer.Id);

                entity.Name    = customer.Name;
                entity.Address = customer.Address;
                entity.Phone   = customer.Phone;

                context.SaveChanges();
            }
        }
        public void DeleteCustomersByAddress(string address)
        {
            using (RetailEntities context = new RetailEntities())
              {
            List<Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList();

            foreach (var entity in entities)
            {
              context.Set<Customer>().Remove(entity);
            }

            context.SaveChanges();
              }
        }
Exemplo n.º 4
0
        public T Add(T entity)
        {
            T result = default(T);

            using (ObjectContext context = new RetailEntities())
            {
                context.AddObject(context.GetEntitySet <T>().Name, entity);
                context.SaveChanges();

                result = entity;
            }

            return(result);
        }
Exemplo n.º 5
0
        public void RemoveAll()
        {
            using (ObjectContext context = new RetailEntities())
            {
                ObjectQuery <T> entities = context.CreateQuery <T>("[" + context.GetEntitySet <T>().Name + "]");

                foreach (var entity in entities)
                {
                    context.DeleteObject(entity);
                }

                context.SaveChanges();
            }
        }
        public void DeleteCustomersByAddress(string address)
        {
            using (RetailEntities context = new RetailEntities())
            {
                List <Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList();

                foreach (var entity in entities)
                {
                    context.DeleteObject(entity);
                }

                context.SaveChanges();
            }
        }
        public void DeleteAllCustomers()
        {
            using (RetailEntities context = new RetailEntities())
              {
            List<Customer> entities = context.Customers.AsQueryable().ToList();

            foreach (var entity in entities)
            {
              context.Set<Customer>().Remove(entity);
            }

            context.SaveChanges();

            //context.ExecuteStoreCommand("TRUNCATE TABLE [RETAIL].[STORE].[Customer]");
              }
        }
        public List <DomainModels.Customer> GetCustomersByAddress(string address)
        {
            using (RetailEntities context = new RetailEntities())
            {
                List <Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList();
                List <DomainModels.Customer> customers = new List <DomainModels.Customer>();

                foreach (var entity in entities)
                {
                    DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity);
                    customers.Add(customer);
                }

                return(customers);
            }
        }
        public List<DomainModels.Customer> GetCustomersByAddress(string address)
        {
            using (RetailEntities context = new RetailEntities())
              {
            List<Customer> entities = context.Customers.AsQueryable().Where(c => c.Address == address).ToList();
            List<DomainModels.Customer> customers = new List<DomainModels.Customer>();

            foreach (var entity in entities)
            {
              DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity);
              customers.Add(customer);
            }

            return customers;
              }
        }
Exemplo n.º 10
0
        public IList <T> GetAll()
        {
            IList <T> list = new List <T>();

            using (ObjectContext context = new RetailEntities())
            {
                ObjectQuery <T> entities = context.CreateQuery <T>("[" + context.GetEntitySet <T>().Name + "]");

                foreach (var entity in entities)
                {
                    list.Add(entity);
                }
            }

            return(list);
        }
        public void DeleteAllCustomers()
        {
            using (RetailEntities context = new RetailEntities())
            {
                List <Customer> entities = context.Customers.AsQueryable().ToList();

                foreach (var entity in entities)
                {
                    context.DeleteObject(entity);
                }

                context.SaveChanges();

                //context.ExecuteStoreCommand("TRUNCATE TABLE [RETAIL].[STORE].[Customer]");
            }
        }
Exemplo n.º 12
0
        public T Update(T entity)
        {
            T result = default(T);

            using (ObjectContext context = new RetailEntities())
            {
                EntityKey key = context.CreateEntityKey(entity.EntityKey.EntitySetName, entity);

                object originEntity;
                if (context.TryGetObjectByKey(key, out originEntity))
                {
                    context.ApplyCurrentValues(key.EntitySetName, entity);
                    context.SaveChanges();
                }

                result = entity;
            }

            return(result);
        }
        static void Main(string[] args)
        {
            // =============== 构造数据 ===============
              Console.ForegroundColor = ConsoleColor.Green;

              int customerCount = 10000;

              List<Customer> customers = new List<Customer>();
              for (int i = 0; i < customerCount; i++)
              {
            Customer customer = new Customer()
            {
              Name = "Dennis Gao" + i,
              Address = "Beijing" + i,
              Phone = "18888888888" + i,
            };
            customers.Add(customer);

            Console.Write(".");
              }

              Console.WriteLine();

              try
              {
            // =============== 插入数据 ===============
            Console.WriteLine(string.Format(
              "Begin to insert {0} customers into database...",
              customerCount));

            Stopwatch watch = Stopwatch.StartNew();

            using (RetailEntities context = new RetailEntities())
            {
              context.BulkInsert(customers);
              context.SaveChanges();
            }

            watch.Stop();
            Console.WriteLine(string.Format(
              "Done, {0} customers are inserted, cost {1} milliseconds.",
              customerCount, watch.ElapsedMilliseconds));
              }
              catch (Exception ex)
              {
            Console.WriteLine(FlattenException(ex));
              }

              Console.WriteLine("=====================================");

              // =============== 查询结果 ===============
              try
              {
            using (RetailEntities context = new RetailEntities())
            {
              int countOfCustomers = context.Customers.AsQueryable().Count();
              Console.WriteLine(string.Format(
            "Now, we have {0} customers.", countOfCustomers));
            }
              }
              catch (Exception ex)
              {
            Console.WriteLine(FlattenException(ex));
              }

              Console.WriteLine("=====================================");

              // =============== 清理 ===============
              Console.WriteLine();
              Console.WriteLine("Press any key to close...");
              Console.ReadKey();
        }
        static void Main(string[] args)
        {
            // =============== 构造数据 ===============
            Console.ForegroundColor = ConsoleColor.Green;

            int customerCount = 10000;

            List <Customer> customers = new List <Customer>();

            for (int i = 0; i < customerCount; i++)
            {
                Customer customer = new Customer()
                {
                    Name    = "Dennis Gao" + i,
                    Address = "Beijing" + i,
                    Phone   = "18888888888" + i,
                };
                customers.Add(customer);

                Console.Write(".");
            }

            Console.WriteLine();

            try
            {
                // =============== 插入数据 ===============
                Console.WriteLine(string.Format(
                                      "Begin to insert {0} customers into database...",
                                      customerCount));

                Stopwatch watch = Stopwatch.StartNew();

                using (RetailEntities context = new RetailEntities())
                {
                    context.BulkInsert(customers);
                    context.SaveChanges();
                }

                watch.Stop();
                Console.WriteLine(string.Format(
                                      "Done, {0} customers are inserted, cost {1} milliseconds.",
                                      customerCount, watch.ElapsedMilliseconds));
            }
            catch (Exception ex)
            {
                Console.WriteLine(FlattenException(ex));
            }

            Console.WriteLine("=====================================");

            // =============== 查询结果 ===============
            try
            {
                using (RetailEntities context = new RetailEntities())
                {
                    int countOfCustomers = context.Customers.AsQueryable().Count();
                    Console.WriteLine(string.Format(
                                          "Now, we have {0} customers.", countOfCustomers));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(FlattenException(ex));
            }

            Console.WriteLine("=====================================");

            // =============== 清理 ===============
            Console.WriteLine();
            Console.WriteLine("Press any key to close...");
            Console.ReadKey();
        }
Exemplo n.º 15
0
        static void Main(string[] args)
        {
            ICustomerRepository customerRepository = new CustomerRepository();

            // =============== 增 ===============
            Console.ForegroundColor = ConsoleColor.Red;

            DomainModels.Customer customer1 = new DomainModels.Customer()
            {
                Name    = "Dennis Gao",
                Address = "Beijing",
                Phone   = "18888888888",
            };
            DomainModels.Customer customer2 = new DomainModels.Customer()
            {
                //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空
                Address = "Beijing",
                Phone   = "16666666666",
            };

            try
            {
                Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1);
                Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2);

                using (RetailEntities context = new RetailEntities())
                {
                    context.Customers.Add(entity1);
                    context.Customers.Add(entity2);

                    using (var transactionScope = new TransactionScope(
                               TransactionScopeOption.RequiresNew,
                               new TransactionOptions()
                    {
                        IsolationLevel = IsolationLevel.ReadUncommitted
                    }))
                    {
                        context.SaveChanges();
                        transactionScope.Complete();
                    }

                    customer1.Id = entity1.Id;
                    customer2.Id = entity2.Id;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(FlattenException(ex));
            }

            Console.WriteLine(customer1);
            Console.WriteLine(customer2);
            Console.WriteLine("=====================================");

            // =============== 查询回滚结果 ===============
            List <DomainModels.Customer> customers = new List <DomainModels.Customer>();

            try
            {
                using (var transactionScope = new TransactionScope(
                           TransactionScopeOption.RequiresNew,
                           new TransactionOptions()
                {
                    IsolationLevel = IsolationLevel.ReadUncommitted
                }))
                {
                    using (RetailEntities context = new RetailEntities())
                    {
                        List <Customer> entities = context.Customers.AsQueryable().ToList();

                        foreach (var entity in entities)
                        {
                            DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity);
                            customers.Add(customer);
                        }
                    }
                    transactionScope.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(FlattenException(ex));
            }

            foreach (var customer in customers)
            {
                Console.WriteLine(customer);
            }

            // =============== 清理 ===============
            Console.WriteLine();
            Console.WriteLine("Press any key to clean database...");
            Console.ReadKey();

            customerRepository.DeleteAllCustomers();
        }
        static void Main(string[] args)
        {
            ICustomerRepository customerRepository = new CustomerRepository();

              // =============== 增 ===============
              Console.ForegroundColor = ConsoleColor.Red;

              DomainModels.Customer customer1 = new DomainModels.Customer()
              {
            Name = "Dennis Gao",
            Address = "Beijing",
            Phone = "18888888888",
              };
              DomainModels.Customer customer2 = new DomainModels.Customer()
              {
            //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空
            Address = "Beijing",
            Phone = "16666666666",
              };

              try
              {

            Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1);
            Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2);

            using (RetailEntities context = new RetailEntities())
            {
              context.Customers.Add(entity1);
              context.Customers.Add(entity2);

              using (var transactionScope = new TransactionScope(
            TransactionScopeOption.RequiresNew,
            new TransactionOptions()
            {
              IsolationLevel = IsolationLevel.ReadUncommitted
            }))
              {
            context.SaveChanges();
            transactionScope.Complete();
              }

              customer1.Id = entity1.Id;
              customer2.Id = entity2.Id;
            }
              }
              catch (Exception ex)
              {
            Console.WriteLine(FlattenException(ex));
              }

              Console.WriteLine(customer1);
              Console.WriteLine(customer2);
              Console.WriteLine("=====================================");

              // =============== 查询回滚结果 ===============
              List<DomainModels.Customer> customers = new List<DomainModels.Customer>();

              try
              {
            using (var transactionScope = new TransactionScope(
              TransactionScopeOption.RequiresNew,
              new TransactionOptions()
              {
            IsolationLevel = IsolationLevel.ReadUncommitted
              }))
            {
              using (RetailEntities context = new RetailEntities())
              {
            List<Customer> entities = context.Customers.AsQueryable().ToList();

            foreach (var entity in entities)
            {
              DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity);
              customers.Add(customer);
            }
              }
              transactionScope.Complete();
            }
              }
              catch (Exception ex)
              {
            Console.WriteLine(FlattenException(ex));
              }

              foreach (var customer in customers)
              {
            Console.WriteLine(customer);
              }

              // =============== 清理 ===============
              Console.WriteLine();
              Console.WriteLine("Press any key to clean database...");
              Console.ReadKey();

              customerRepository.DeleteAllCustomers();
        }
        static void Main(string[] args)
        {
            ICustomerRepository customerRepository = new CustomerRepository();

            // =============== 增 ===============
            Console.ForegroundColor = ConsoleColor.Red;

            DomainModels.Customer customer1 = new DomainModels.Customer()
            {
                Name    = "Dennis Gao",
                Address = "Beijing",
                Phone   = "18888888888",
            };
            DomainModels.Customer customer2 = new DomainModels.Customer()
            {
                //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空
                Address = "Beijing",
                Phone   = "16666666666",
            };

            //Task t = Task.Factory.StartNew(() =>
            //{
            //  int i = 0;
            //  while (i < 100)
            //  {
            //    List<DomainModels.Customer> customers = new List<DomainModels.Customer>();

            //    using (var transactionScope = new TransactionScope(
            //      TransactionScopeOption.RequiresNew,
            //      new TransactionOptions()
            //      {
            //        IsolationLevel = IsolationLevel.ReadUncommitted
            //      }))
            //    {
            //      try
            //      {
            //        using (RetailEntities context = new RetailEntities())
            //        {
            //          List<Customer> entities = context.Customers.AsQueryable().ToList();

            //          foreach (var entity in entities)
            //          {
            //            DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity);
            //            customers.Add(customer);
            //          }
            //        }
            //      }
            //      catch (Exception ex)
            //      {
            //        Console.WriteLine(FlattenException(ex));
            //      }
            //      transactionScope.Complete();
            //    }

            //    Console.WriteLine("-----> " + i + " times");
            //    foreach (var customer in customers)
            //    {
            //      Console.WriteLine(customer);
            //    }

            //    i++;
            //    Thread.Sleep(1000);
            //  }
            //});

            try
            {
                using (var transactionScope = new TransactionScope(
                           TransactionScopeOption.RequiresNew))
                {
                    Customer entity1 = Mapper.Map <DomainModels.Customer, Customer>(customer1);
                    Customer entity2 = Mapper.Map <DomainModels.Customer, Customer>(customer2);

                    using (RetailEntities context = new RetailEntities())
                    {
                        context.Customers.Add(entity1);
                        context.SaveChanges(); // 顺利提交
                        context.Customers.Add(entity2);
                        context.SaveChanges(); // 提交时将抛出异常

                        customer1.Id = entity1.Id;
                        customer2.Id = entity2.Id;
                    }

                    transactionScope.Complete();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(FlattenException(ex));
            }

            Console.WriteLine(customer1);
            Console.WriteLine(customer2);
            Console.WriteLine("=====================================");

            // =============== 查询回滚结果 ===============
            List <DomainModels.Customer> getCustomers = new List <DomainModels.Customer>();

            using (var transactionScope = new TransactionScope(
                       TransactionScopeOption.RequiresNew,
                       new TransactionOptions()
            {
                IsolationLevel = IsolationLevel.ReadUncommitted
            }))
            {
                try
                {
                    using (RetailEntities context = new RetailEntities())
                    {
                        List <Customer> entities = context.Customers.AsQueryable().ToList();

                        foreach (var entity in entities)
                        {
                            DomainModels.Customer customer = Mapper.Map <Customer, DomainModels.Customer>(entity);
                            getCustomers.Add(customer);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(FlattenException(ex));
                }

                transactionScope.Complete();
            }

            foreach (var customer in getCustomers)
            {
                Console.WriteLine(customer);
            }

            // =============== 清理 ===============
            Console.WriteLine();
            Console.WriteLine("Press any key to clean database...");
            Console.ReadKey();

            customerRepository.DeleteAllCustomers();
        }
        public void UpdateCustomer(DomainModels.Customer customer)
        {
            using (RetailEntities context = new RetailEntities())
              {
            Customer entity = context.Customers.AsQueryable().Single(c => c.Id == customer.Id);

            entity.Name = customer.Name;
            entity.Address = customer.Address;
            entity.Phone = customer.Phone;

            context.SaveChanges();
              }
        }
        public void InsertCustomer(DomainModels.Customer customer)
        {
            using (RetailEntities context = new RetailEntities())
              {
            Customer entity = Mapper.Map<DomainModels.Customer, Customer>(customer);
            context.Customers.Add(entity);
            context.SaveChanges();

            customer.Id = entity.Id;
              }
        }
        static void Main(string[] args)
        {
            ICustomerRepository customerRepository = new CustomerRepository();

              // =============== 增 ===============
              Console.ForegroundColor = ConsoleColor.Red;

              DomainModels.Customer customer1 = new DomainModels.Customer()
              {
            Name = "Dennis Gao",
            Address = "Beijing",
            Phone = "18888888888",
              };
              DomainModels.Customer customer2 = new DomainModels.Customer()
              {
            //Name = "Degang Guo", // 创造一个无效的对象,此处客户名称不能为空
            Address = "Beijing",
            Phone = "16666666666",
              };

              //Task t = Task.Factory.StartNew(() =>
              //{
              //  int i = 0;
              //  while (i < 100)
              //  {
              //    List<DomainModels.Customer> customers = new List<DomainModels.Customer>();

              //    using (var transactionScope = new TransactionScope(
              //      TransactionScopeOption.RequiresNew,
              //      new TransactionOptions()
              //      {
              //        IsolationLevel = IsolationLevel.ReadUncommitted
              //      }))
              //    {
              //      try
              //      {
              //        using (RetailEntities context = new RetailEntities())
              //        {
              //          List<Customer> entities = context.Customers.AsQueryable().ToList();

              //          foreach (var entity in entities)
              //          {
              //            DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity);
              //            customers.Add(customer);
              //          }
              //        }
              //      }
              //      catch (Exception ex)
              //      {
              //        Console.WriteLine(FlattenException(ex));
              //      }
              //      transactionScope.Complete();
              //    }

              //    Console.WriteLine("-----> " + i + " times");
              //    foreach (var customer in customers)
              //    {
              //      Console.WriteLine(customer);
              //    }

              //    i++;
              //    Thread.Sleep(1000);
              //  }
              //});

              try
              {
            using (var transactionScope = new TransactionScope(
              TransactionScopeOption.RequiresNew))
            {
              Customer entity1 = Mapper.Map<DomainModels.Customer, Customer>(customer1);
              Customer entity2 = Mapper.Map<DomainModels.Customer, Customer>(customer2);

              using (RetailEntities context = new RetailEntities())
              {
            context.Customers.Add(entity1);
            context.SaveChanges(); // 顺利提交
            context.Customers.Add(entity2);
            context.SaveChanges(); // 提交时将抛出异常

            customer1.Id = entity1.Id;
            customer2.Id = entity2.Id;
              }

              transactionScope.Complete();
            }
              }
              catch (Exception ex)
              {
            Console.WriteLine(FlattenException(ex));
              }

              Console.WriteLine(customer1);
              Console.WriteLine(customer2);
              Console.WriteLine("=====================================");

              // =============== 查询回滚结果 ===============
              List<DomainModels.Customer> getCustomers = new List<DomainModels.Customer>();

              using (var transactionScope = new TransactionScope(
            TransactionScopeOption.RequiresNew,
            new TransactionOptions()
            {
              IsolationLevel = IsolationLevel.ReadUncommitted
            }))
              {
            try
            {
              using (RetailEntities context = new RetailEntities())
              {
            List<Customer> entities = context.Customers.AsQueryable().ToList();

            foreach (var entity in entities)
            {
              DomainModels.Customer customer = Mapper.Map<Customer, DomainModels.Customer>(entity);
              getCustomers.Add(customer);
            }
              }
            }
            catch (Exception ex)
            {
              Console.WriteLine(FlattenException(ex));
            }

            transactionScope.Complete();
              }

              foreach (var customer in getCustomers)
              {
            Console.WriteLine(customer);
              }

              // =============== 清理 ===============
              Console.WriteLine();
              Console.WriteLine("Press any key to clean database...");
              Console.ReadKey();

              customerRepository.DeleteAllCustomers();
        }