예제 #1
0
        public void Select_WithCallBack_Refresh__With_Key_Customers()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select 'Extra' as ExtraColumn, *, 'Extra' as ExtraColumn from Customers")
                .WithCallBack(e => new { e.ID, e.RV, e.LastName, e.FirstName })
                .Refresh(context.Customers);

                connection.Close();
            }

            context.Customers[0].NationalIdentityNumber = "9999999999";

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select 'Extra' as ExtraColumn, *, 'Extra' as ExtraColumn from Customers")
                .WithCallBack(e => new { e.ID, e.NationalIdentityNumber })
                .Refresh(context.Customers);

                connection.Close();
            }
        }
예제 #2
0
파일: SelectTest.cs 프로젝트: ali-zare/Abi
        public void Select_FirstOrDefaultAsync_Prepared_WithParameters_NonEntity_Customers()
        {
            NonEntity.Customer customer;

            EntityDatabaseConnection connection = GetConnection();

            connection.Open();

            var t = Database.Connect(connection).Prepared()
                    .WithParameters(new { CustomerID = 16 })
                    .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                          from Customers c
                                                          where c.ID = @CustomerID

                                                          waitfor delay '00:00:01'")
                    .FirstOrDefaultAsync <NonEntity.Customer>()
                    .ContinueWith(task =>
            {
                customer = task.Result;
                connection.Close();
            });


            // do some work here, then wait for select command to be returned ...

            t.Wait();
        }
예제 #3
0
        private void Truncate_And_Insert()
        {
            EntityDatabaseConnection connection = GetConnection();

            connection.Open();

            Database.Connect(connection)
            .Prepared()
            .WithParameters(new { ID = long.MaxValue, PostalCode = "1234554321", Telephone = "99911223344", Cellphone = "09991112233" })
            .Query(@"set nocount on;

                             truncate table Shipments;

                             set identity_insert Shipments on;

                             insert into Shipments(ID, PostalCode, Telephone, Cellphone)
                                        values (@ID, @PostalCode, @Telephone, @Cellphone);

                             set identity_insert Shipments off;

                           ")
            .Execute();

            connection.Close();
        }
예제 #4
0
        public void Select_SingleOrDefault_Adhoc_WithParameters_NonEntity_OrderShippingDate()
        {
            DateTime orderShippingDate;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                orderShippingDate = Database.Connect(connection).Prepared()
                                    .WithParameters(new { OrderID = 505L })
                                    .Select(@"select o.ShippingDate
                                                                          from Orders o
                                                                          where o.ID = @OrderID")
                                    .SingleOrDefault <DateTime>();

                orderShippingDate = Database.Connect(connection).Prepared()
                                    .WithParameters(new { OrderID = 505L })
                                    .Select(@"select o.ShippingDate
                                                                          from Orders o
                                                                          where o.ID = @OrderID")
                                    .SingleOrDefault <DateTime>();

                connection.Close();
            }
        }
예제 #5
0
파일: SelectTest.cs 프로젝트: ali-zare/Abi
        public void Select_SingleAsync_Prepared_WithParameters_NonEntity_CustomerFirstPartnerID()
        {
            int?customerFirstPartnerID;

            EntityDatabaseConnection connection = GetConnection();

            connection.Open();

            var t = Database.Connect(connection).Prepared()
                    .WithParameters(new { CustomerID = 16 })
                    .Select(@"select c.FirstPartnerID
                                                          from Customers c
                                                          where c.ID = @CustomerID

                                                          waitfor delay '00:00:01'")
                    .SingleAsync <int?>()
                    .ContinueWith(task =>
            {
                customerFirstPartnerID = task.Result;
                connection.Close();
            });

            // do some work here, then wait for select command to be returned ...

            t.Wait();
        }
예제 #6
0
        public void Select_ToArray_Prepared_WithoutParameters_NonEntity_Anonymous_Customers()
        {
            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                var customers = Database.Connect(connection).Prepared()
                                .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                                      from Customers c")
                                .ToArray(() => new { ID = default(int), RV = default(Types.RowVersion), FName = default(string), LName = default(string), NIN = default(string) });

                connection.Close();
            }
        }
예제 #7
0
        public void Select_FirstOrDefault_Adhoc_WithParameters_NonEntity_Anonymous_Customers()
        {
            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                var customer = Database.Connect(connection).Adhoc()
                               .WithParameters(new { CustomerID = 16 })
                               .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                                     from Customers c
                                                                     where c.ID = @CustomerID")
                               .FirstOrDefault(() => new { ID = default(int), RV = default(Types.RowVersion), FName = default(string), LName = default(string), NIN = default(string) });

                connection.Close();
            }
        }
예제 #8
0
        public void Select_ToArray_Prepared_WithoutParameters_NonEntity_Customers()
        {
            NonEntity.Customer[] customers;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                customers = Database.Connect(connection).Prepared()
                            .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                                  from Customers c")
                            .ToArray <NonEntity.Customer>();

                connection.Close();
            }
        }
예제 #9
0
        public void Select_FirstOrDefault_Adhoc_WithoutParameters_NonEntity_Customers()
        {
            NonEntity.Customer customer;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                customer = Database.Connect(connection).Adhoc()
                           .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                                 from Customers c")
                           .FirstOrDefault <NonEntity.Customer>();

                connection.Close();
            }
        }
예제 #10
0
        public void Select_WithoutCallBack_Fill_Customers()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select 'Extra' as ExtraColumn, *, 'Extra' as ExtraColumn from Customers")
                .Fill(context.Customers);

                connection.Close();
            }
        }
예제 #11
0
        public void SaveBatch_WithoutCallBack()
        {
            void Save(EnterpriseContext context)
            {
                EntityDatabaseConnection connection = GetConnection();

                connection.Open();
                connection.BeginTransaction(IsolationLevel.Snapshot);

                (context, connection).WithoutCallBack().Save();

                connection.CommitTransaction();
                connection.Close();
            }

            Challenge(Save);
        }
예제 #12
0
        public void Save_WithCheckConcurrency()
        {
            void Save(EnterpriseContext context)
            {
                EntityDatabaseConnection connection = GetConnection();

                connection.Open();
                connection.BeginTransaction(IsolationLevel.Snapshot);

                (context, connection).WithCheckConcurrency().Save(false);

                connection.CommitTransaction();
                connection.Close();
            }

            Challenge(Save);
        }
예제 #13
0
        public void Select_WithCallBack_Products()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Product>()
                .Select("select * from Products")
                .WithCallBack(e => new { e.ID, e.Name, e.Manufacturer, e.Length, e.Width, e.Height, e.Weight, e.Color, e.Power, e.EnergyEfficiency, e.Material, e.Quality, e.Kind, e.Image, e.RV })
                .Fill(context.Products);

                connection.Close();
            }
        }
예제 #14
0
        public void Select_WithCallBack_Merge_Without_Key_Customers()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select 'Extra' as ExtraColumn, *, 'Extra' as ExtraColumn from Customers")
                .WithCallBack(e => new { e.RV, e.LastName, e.FirstName })
                .Merge(context.Customers);

                connection.Close();
            }
        }
예제 #15
0
        public void Select_WithCallBack_Orders()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Order>()
                .Select("select * from Orders")
                .WithCallBack(e => new { e.ID, e.CustomerID, e.ReceiveDate, e.ShippingDate, e.RV })
                .Fill(context.Orders);

                connection.Close();
            }
        }
예제 #16
0
        public void Select_WithCallBack_OrderDetails()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <OrderDetail>()
                .Select("select * from OrderDetails")
                .WithCallBack(e => new { e.ID, e.RV, e.Amount, e.Fee, e.OrderID })                                  // OrderID is not null column, therefore it cann't be omitted in query, and must be loaded in model
                .Fill(context.OrderDetails);

                connection.Close();
            }
        }
예제 #17
0
        public void Select_WithCallBack_Fill_Customers_FiveTime()
        {
            EnterpriseContext context1 = new EnterpriseContext();
            EnterpriseContext context2 = new EnterpriseContext();
            EnterpriseContext context3 = new EnterpriseContext();
            EnterpriseContext context4 = new EnterpriseContext();
            EnterpriseContext context5 = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select * from Customers")
                .WithCallBack(e => new { e.ID, e.FirstName, e.LastName, e.NationalIdentityNumber, e.RV })                                  // Same As context4
                .Fill(context1.Customers);

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select * from Customers")
                .WithCallBack(e => new { e.ID, e.FirstName, e.LastName, e.RV })                                  // Same As context5
                .Fill(context2.Customers);

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select * from Customers")
                .WithCallBack(e => new { e.ID, e.RV, e.FirstName, e.LastName, e.NationalIdentityNumber })
                .Fill(context3.Customers);

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select * from Customers")
                .WithCallBack(e => new { e.ID, e.FirstName, e.LastName, e.NationalIdentityNumber, e.RV })                                  // Same As context1
                .Fill(context4.Customers);

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Select("select * from Customers")
                .WithCallBack(e => new { e.ID, e.FirstName, e.LastName, e.RV })                                  // Same As context2
                .Fill(context5.Customers);

                connection.Close();
            }
        }
예제 #18
0
        public void Select_SingleOrDefault_Adhoc_WithParameters_NonEntity_ProductImage()
        {
            byte[] productImage;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                productImage = Database.Connect(connection).Prepared()
                               .WithParameters(new { ProductID = 6005 })
                               .Select(@"select p.Image
                                                                     from Products p
                                                                     where p.ID = @ProductID")
                               .SingleOrDefault <byte[]>();

                connection.Close();
            }
        }
예제 #19
0
        public void Select_SingleOrDefault_Adhoc_WithParameters_NonEntity_OrderRV()
        {
            Types.RowVersion orderRV;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                orderRV = Database.Connect(connection).Prepared()
                          .WithParameters(new { OrderID = 505L })
                          .Select(@"select o.RV
                                                                from Orders o
                                                                where o.ID = @OrderID")
                          .SingleOrDefault <Types.RowVersion>();

                connection.Close();
            }
        }
예제 #20
0
        public void Select_SingleOrDefault_Prepared_WithParameters_NonEntity_CustomerNIN()
        {
            string customerNIN;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                customerNIN = Database.Connect(connection).Prepared()
                              .WithParameters(new { CustomerID = 16 })
                              .Select(@"select c.NIN
                                                                    from Customers c
                                                                    where c.ID = @CustomerID")
                              .SingleOrDefault <string>();

                connection.Close();
            }
        }
예제 #21
0
        public void Select_SingleOrDefault_Prepared_WithParameters_NonEntity_CustomerFirstPartnerID()
        {
            int?customerFirstPartnerID;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                customerFirstPartnerID = Database.Connect(connection).Prepared()
                                         .WithParameters(new { CustomerID = 16 })
                                         .Select(@"select c.FirstPartnerID
                                                                          from Customers c
                                                                          where c.ID = @CustomerID")
                                         .SingleOrDefault <int?>();

                connection.Close();
            }
        }
예제 #22
0
        public void Select_ToArray_Adhoc_WithParameters_NonEntity_Customers()
        {
            NonEntity.Customer[] customers;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                customers = Database.Connect(connection).Adhoc()
                            .WithParameters(new { CustomerID = 16 })
                            .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                                  from Customers c
                                                                  where c.ID = @CustomerID")
                            .ToArray <NonEntity.Customer>();

                connection.Close();
            }
        }
예제 #23
0
        public void Select_FirstOrDefault_Prepared_WithParameters_NonEntity_Customers()
        {
            NonEntity.Customer customer;

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                customer = Database.Connect(connection).Prepared()
                           .WithParameters(new { CustomerID = 16 })
                           .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                                 from Customers c
                                                                 where c.ID = @CustomerID")
                           .FirstOrDefault <NonEntity.Customer>();

                connection.Close();
            }
        }
예제 #24
0
        private void Truncate_And_ReseedIdentity()
        {
            EntityDatabaseConnection connection = GetConnection();

            connection.Open();

            Database.Connect(connection)
            .Prepared()
            .Query(@"set nocount on;

                             truncate table Shipments;

                             dbcc checkident (Shipments);

                           ")
            .Execute();

            connection.Close();
        }
예제 #25
0
        public void Select_Prepared_WithoutParameters_WithCallBack_Fill_Customers()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Prepared()
                .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                           from Customers c")
                .WithCallBack(e => new { e.ID, e.FirstName, e.LastName, e.NationalIdentityNumber, e.RV })
                .Fill(context.Customers);

                connection.Close();
            }
        }
예제 #26
0
        public void Select_Prepared_WithParameters_WithoutCallBack_Fill_Customers()
        {
            EnterpriseContext context = new EnterpriseContext();

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>()
                .Prepared()
                .WithParameters(new { CustomerID = 16 })
                .Select(@"select 'Extra' as ExtraColumn, c.*, 'Extra' as ExtraColumn 
                                                           from Customers c
                                                           where c.ID = @CustomerID")
                .Fill(context.Customers);

                connection.Close();
            }
        }
예제 #27
0
        public void Save_Data_WithEvent_WithCheckConcurrency()
        {
            void EntitySaving(EntityDatabaseContext <EnterpriseContext> sender, EntitySavingEventArgs <EnterpriseContext> e)
            {
                TestContext.WriteLine($"Saving {e.Entity.State}, {e.Entity}");
            }

            void Setter_EntitySaved(EntityDatabaseContext <EnterpriseContext> sender, EntitySavedEventArgs <EnterpriseContext> e)
            {
                TestContext.WriteLine($"Saved {e.State}, {e.Entity}");
            }

            void Save(EnterpriseContext context)
            {
                EntityDatabaseConnection connection = GetConnection();

                connection.Open();
                connection.BeginTransaction(IsolationLevel.Snapshot);

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Context(context)
                .Table <Customer>()
                .Insert().WithCallBack(e => new { e.RV })
                .Update().WithCallBack(e => new { e.RV }).WithCheckConcurrency(e => new { e.RV })
                .Delete().WithCheckConcurrency(e => new { e.RV })
                .Table <Order>()
                .Insert().WithCallBack(e => new { e.RV })
                .Update().WithCallBack(e => new { e.RV }).WithCheckConcurrency(e => new { e.RV })
                .Delete().WithCheckConcurrency(e => new { e.RV })
                .Event()
                .Set(setter => setter.EntitySaving += EntitySaving)
                .Set(setter => setter.EntitySaved  += Setter_EntitySaved)
                .Save(false);

                connection.CommitTransaction();
                connection.Close();
            }

            Data(Save);
        }
예제 #28
0
파일: QueryTest.cs 프로젝트: ali-zare/Abi
        public void Query_Execute_Adhoc_WithParameters_Insert_Customer_Update_Order()
        {
            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();
                connection.BeginTransaction(IsolationLevel.Snapshot);

                Database.Connect(connection).Adhoc()
                .WithParameters(new { OrderID = 509, FName = "New-FirstName", LName = "New-LastName", NIN = "7777777777" })
                .Query(@"insert into Customers(FName, LName, NIN) 
                                                                   values (@FName, @LName, @NIN);
                                                    
                                                     update Orders
                                                        set CustomerID = scope_identity()
                                                     where ID = @OrderID;
                                                    ")
                .Execute();

                connection.CommitTransaction();
                connection.Close();
            }
        }
예제 #29
0
        private void Data(Action <EnterpriseContext> save)
        {
            EnterpriseContext context = new EnterpriseContext();

            Customer ali = new Customer();

            ali.FirstName = "Ali";
            ali.LastName  = "Zare";
            context.Customers.Add(ali);

            Customer amir = new Customer();

            amir.FirstName = "Amir";
            context.Customers.Add(amir);

            Customer sam = new Customer();

            context.Customers.Add(sam);


            Order order1 = new Order();

            order1.ReceiveDate  = "1398/05/06".ToDateTime();
            order1.ShippingDate = "1398/06/06".ToDateTime();
            context.Orders.Add(order1);

            Order order2 = new Order();

            context.Orders.Add(order2);

            Order order3 = new Order();

            context.Orders.Add(order3);


            #region insert amir, sam, order2 and order3

            using (EntityDatabaseConnection connection = GetConnection())
            {
                connection.Open();


                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Customer>().Insert().WithCallBack(e => new { e.RV })
                .Write(amir, sam);

                EntityDatabase <EnterpriseContext> .Connect(connection)
                .Table <Order>().Insert().WithCallBack(e => new { e.RV })
                .Write(order2, order3);

                connection.Close();
            }

            #endregion insert amir, sam, order2 and order3

            amir.LastName = "Zare";

            context.Customers.Remove(sam);

            order2.CustomerID = ali.ID;

            context.Orders.Remove(order3);



            #region check

            ali.CheckAdded();
            amir.CheckModified();
            sam.CheckDeleted();

            order1.CheckAdded();
            order2.CheckModified();
            order3.CheckDeleted();

            context.Changes.CheckCount(6)
            .CheckFound(ali)
            .CheckFound(amir)
            .CheckFound(sam)
            .CheckFound(order1)
            .CheckFound(order2)
            .CheckFound(order3);

            #endregion check

            bool has_concurrency_conflict = false;

            try
            {
                context.Execute(save);
            }
            catch (EntityDatabaseConcurrencyException)
            {
                has_concurrency_conflict = true;
            }

            #region check

            if (has_concurrency_conflict)
            {
                ali.CheckAdded();
                amir.CheckModified();
                sam.CheckDeleted();

                order1.CheckAdded();
                order2.CheckModified();
                order3.CheckDeleted();

                context.Changes.CheckCount(6)
                .CheckFound(ali)
                .CheckFound(amir)
                .CheckFound(sam)
                .CheckFound(order1)
                .CheckFound(order2)
                .CheckFound(order3);
            }
            else
            {
                ali.CheckUnchanged();
                amir.CheckUnchanged();
                sam.CheckDetached();

                order1.CheckUnchanged();
                order2.CheckUnchanged();
                order3.CheckDetached();

                context.Changes.CheckCount(0);
            }

            #endregion check
        }
예제 #30
0
        private void Conflict(Action <EnterpriseContext> save)
        {
            EnterpriseContext context = new EnterpriseContext();

            #region step1: define some new data ...

            Order order = new Order();
            order.ReceiveDate = "1398/06/25".ToDateTime();
            context.Orders.Add(order);

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.Order  = order;
            orderDetail.Amount = 100;
            context.OrderDetails.Add(orderDetail);

            Customer ali = new Customer();
            ali.FirstName = "Ali";
            ali.LastName  = "Zare";
            context.Customers.Add(ali);

            order.Customer = ali;

            Product laptop = new Product();
            laptop.Name = "Laptop Asus N552";
            context.Products.Add(laptop);

            orderDetail.Product = laptop;


            #region check

            laptop.CheckAdded();

            ali.CheckAdded();
            ali.Orders.CheckCount(1).CheckItem(0, order);

            order.CheckAdded();
            order.CustomerID.Check(ali.ID);
            order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail);

            orderDetail.CheckAdded();
            orderDetail.OrderID.Check(order.ID);
            orderDetail.ProductID.Check(laptop.ID);

            context.Changes.CheckCount(4)
            .CheckFound(laptop)
            .CheckFound(ali)
            .CheckFound(order)
            .CheckFound(orderDetail);

            #endregion check

            // save changes
            context.Execute(save);

            #region check

            laptop.CheckUnchanged();

            ali.CheckUnchanged();
            ali.Orders.CheckCount(1).CheckItem(0, order);

            order.CheckUnchanged();
            order.Customer.Check(ali);
            order.CustomerID.Check(ali.ID);
            order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail);

            orderDetail.CheckUnchanged();
            orderDetail.Order.Check(order);
            orderDetail.OrderID.Check(order.ID);
            orderDetail.Product.Check(laptop);
            orderDetail.ProductID.Check(laptop.ID);

            context.Changes.CheckCount(0);

            #endregion check

            #endregion step1: define some new data ...


            // Test Snapshot ...
            #region step2: define some new data and edit previous data ...

            Order order1 = new Order();
            order1.Customer    = ali;
            order1.ReceiveDate = "1398/07/07".ToDateTime();
            context.Orders.Add(order1);

            OrderDetail orderDetail1 = new OrderDetail();
            orderDetail1.Order  = order1;
            orderDetail1.Amount = 500;
            context.OrderDetails.Add(orderDetail1);

            Customer amir = new Customer();
            amir.FirstName = "Amir";
            amir.LastName  = "Zare";
            context.Customers.Add(amir);

            order.Customer = amir;

            Product tv = new Product();
            tv.Name = "Sony NX 40 inch";
            context.Products.Add(tv);

            orderDetail.Product  = tv;
            orderDetail1.Product = laptop;

            #region check

            laptop.CheckUnchanged();

            ali.CheckUnchanged();
            ali.Orders.CheckCount(1).CheckItem(0, order1);

            order.CheckModified();
            order.CustomerID.Check(amir.ID);
            order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail);
            order.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID);

            orderDetail.CheckModified();
            orderDetail.ProductID.Check(tv.ID);
            orderDetail.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.ProductID);

            tv.CheckAdded();

            amir.CheckAdded();
            amir.Orders.CheckCount(1).CheckItem(0, order);

            order1.CheckAdded();
            order1.CustomerID.Check(ali.ID);
            order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1);

            orderDetail1.CheckAdded();
            orderDetail1.OrderID.Check(order1.ID);
            orderDetail1.ProductID.Check(laptop.ID);

            context.Changes.CheckCount(6)
            .CheckFound(tv)
            .CheckFound(amir)
            .CheckFound(order)
            .CheckFound(order1)
            .CheckFound(orderDetail)
            .CheckFound(orderDetail1);

            #endregion check

            bool has_concurrency_conflict = false;

            #region parallel delete

            EntityDatabaseConnection connection = GetConnection();

            connection.Open();

            Database.Connect(connection)
            .Prepared()
            .WithParameters(new { OrderID = order.ID })
            .Query(@"set nocount on;

                             delete 
                             from OrderDetails
                             where OrderID = @OrderID

                             delete 
                             from Orders
                             where ID = @OrderID")
            .Execute();

            connection.Close();

            #endregion parallel delete

            try
            {
                // save changes
                context.Execute(save);
            }
            catch (EntityDatabaseConcurrencyException)
            {
                has_concurrency_conflict = true;
            }

            #region check

            if (has_concurrency_conflict)
            {
                laptop.CheckUnchanged();

                ali.CheckUnchanged();
                ali.Orders.CheckCount(1).CheckItem(0, order1);

                order.CheckModified();
                order.CustomerID.Check(amir.ID);
                order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail);
                order.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID);

                orderDetail.CheckModified();
                orderDetail.ProductID.Check(tv.ID);
                orderDetail.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.ProductID);

                tv.CheckAdded();

                amir.CheckAdded();
                amir.Orders.CheckCount(1).CheckItem(0, order);

                order1.CheckAdded();
                order1.CustomerID.Check(ali.ID);
                order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1);

                orderDetail1.CheckAdded();
                orderDetail1.OrderID.Check(order1.ID);
                orderDetail1.ProductID.Check(laptop.ID);

                context.Changes.CheckCount(6)
                .CheckFound(tv)
                .CheckFound(amir)
                .CheckFound(order)
                .CheckFound(order1)
                .CheckFound(orderDetail)
                .CheckFound(orderDetail1);
            }
            else
            {
                throw new Exception("Expected Exception, Was Not Thrown");

                #region not reachable code

                tv.CheckUnchanged();
                laptop.CheckUnchanged();

                ali.CheckUnchanged();
                ali.Orders.CheckCount(1).CheckItem(0, order1);

                amir.CheckUnchanged();
                amir.Orders.CheckCount(1).CheckItem(0, order);

                order.CheckUnchanged();
                order.Customer.Check(amir);
                order.CustomerID.Check(amir.ID);
                order.OrderDetails.CheckCount(1).CheckItem(0, orderDetail);

                order1.CheckUnchanged();
                order1.Customer.Check(ali);
                order1.CustomerID.Check(ali.ID);
                order1.OrderDetails.CheckCount(1).CheckItem(0, orderDetail1);

                orderDetail.CheckUnchanged();
                orderDetail.Order.Check(order);
                orderDetail.OrderID.Check(order.ID);
                orderDetail.Product.Check(tv);
                orderDetail.ProductID.Check(tv.ID);

                orderDetail1.CheckUnchanged();
                orderDetail1.Order.Check(order1);
                orderDetail1.OrderID.Check(order1.ID);
                orderDetail1.Product.Check(laptop);
                orderDetail1.ProductID.Check(laptop.ID);

                context.Changes.CheckCount(0);

                #endregion not reachable code
            }

            #endregion check

            #endregion step2: define some new data and edit previous data ...
        }