Exemplo n.º 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();
            }
        }
Exemplo n.º 2
0
        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();
        }
Exemplo n.º 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();
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 5
0
        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();
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 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);
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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();
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 28
0
        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();
            }
        }
Exemplo n.º 29
0
        private void ConflictOnUpdate(Action <EnterpriseContext> save)
        {
            EnterpriseContext context = new EnterpriseContext();

            // Add order1
            Order order1 = new Order();

            context.Orders.Add(order1);

            // Add orderDetail1
            OrderDetail orderDetail1 = new OrderDetail();

            orderDetail1.Order = order1;
            context.OrderDetails.Add(orderDetail1);

            // Add customer1
            Customer customer1 = new Customer();

            customer1.FirstName = "customer1";
            context.Customers.Add(customer1);

            customer1.Orders.Add(order1);

            #region check

            order1.CheckAdded();
            customer1.CheckAdded();
            orderDetail1.CheckAdded();

            context.Changes.CheckCount(3)
            .CheckFound(order1)
            .CheckFound(customer1)
            .CheckFound(orderDetail1);

            #endregion check

            // save changes
            context.Execute(save);

            #region check

            context.Changes.CheckCount(0);

            order1.CheckUnchanged();
            customer1.CheckUnchanged();
            orderDetail1.CheckUnchanged();

            customer1.Orders.CheckCount(1).CheckItem(0, order1);

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

            orderDetail1.Order.Check(order1);
            orderDetail1.OrderID.Check(order1.ID);

            order1.GetChangedProperties().CheckCount(0);
            customer1.GetChangedProperties().CheckCount(0);
            orderDetail1.GetChangedProperties().CheckCount(0);

            #endregion check

            // Add shipment1
            Shipment shipment1 = new Shipment();
            context.Shipments.Add(shipment1);

            // Add order2
            Order order2 = new Order();
            context.Orders.Add(order2);

            // Modify orderDetail1
            orderDetail1.Order = order2;

            // Add orderDetail2
            OrderDetail orderDetail2 = new OrderDetail();
            orderDetail2.Order = order2;
            context.OrderDetails.Add(orderDetail2);

            // Modify order1
            order1.Customer = null;

            // Add customer2
            Customer customer2 = new Customer();
            customer2.FirstName = "customer2";
            context.Customers.Add(customer2);

            // Modify customer1
            customer1.SecondPartner = customer2;

            // Modify order1
            order1.Customer = customer2;

            shipment1.Order = order2;

            // Add customer3
            Customer customer3 = new Customer();
            customer3.FirstName = "customer3";
            context.Customers.Add(customer3);

            customer2.FirstPartner = customer3;

            // Delete customer1
            context.Customers.Remove(customer1);

            order2.Customer = customer2;

            // Modify order1
            order1.Customer = customer3;

            #region check

            order2.CheckAdded();
            shipment1.CheckAdded();
            customer2.CheckAdded();
            customer3.CheckAdded();
            orderDetail2.CheckAdded();

            customer1.CheckDeleted();

            order1.CheckModified();
            order1.Customer.Check(customer3);
            order1.CustomerID.Check(customer3.ID);
            order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID);

            orderDetail1.CheckModified();
            orderDetail1.Order.Check(order2);
            orderDetail1.OrderID.Check(order2.ID);
            orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID);

            context.Changes.CheckCount(8)
            .CheckFound(order1)
            .CheckFound(order2)
            .CheckFound(shipment1)
            .CheckFound(customer1)
            .CheckFound(customer2)
            .CheckFound(customer3)
            .CheckFound(orderDetail1)
            .CheckFound(orderDetail2);

            #endregion check

            #region update order1

            async Task update()
            {
                EntityDatabaseConnection connection = GetConnection();

                await connection.OpenAsync();

                connection.BeginTransaction(IsolationLevel.Snapshot);

                await Database.Connect(connection)
                .Prepared()
                .WithParameters(new { OrderID = order1.ID, ShippingDate = "1398/06/30".ToDateTime() })
                .Query(@"set nocount on;

                                       update Orders
                                          set ShippingDate = @ShippingDate
                                       where ID = @OrderID;

                                       waitfor delay '00:00:00.5'

                                      ")
                .ExecuteAsync();

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

            Task task = update();

            #endregion update order1

            try
            {
                // save changes
                context.Execute(save);
            }
            catch (SqlException e) when(e.Number == 3960)
            {
                // it's ok, conflict was occurred
            }

            task.Wait();

            #region check

            order2.CheckAdded();
            shipment1.CheckAdded();
            customer2.CheckAdded();
            customer3.CheckAdded();
            orderDetail2.CheckAdded();

            customer1.CheckDeleted();

            order1.CheckModified();
            order1.Customer.Check(customer3);
            order1.CustomerID.Check(customer3.ID);
            order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID);

            orderDetail1.CheckModified();
            orderDetail1.Order.Check(order2);
            orderDetail1.OrderID.Check(order2.ID);
            orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID);

            context.Changes.CheckCount(8)
            .CheckFound(order1)
            .CheckFound(order2)
            .CheckFound(shipment1)
            .CheckFound(customer1)
            .CheckFound(customer2)
            .CheckFound(customer3)
            .CheckFound(orderDetail1)
            .CheckFound(orderDetail2);

            #endregion check
        }
Exemplo n.º 30
0
        private void ConflictOnDelete(Action <EnterpriseContext> save)
        {
            EnterpriseContext context = new EnterpriseContext();

            // Add order1
            Order order1 = new Order();

            context.Orders.Add(order1);

            // Add orderDetail1
            OrderDetail orderDetail1 = new OrderDetail();

            orderDetail1.Order = order1;
            context.OrderDetails.Add(orderDetail1);

            // Add customer1
            Customer customer1 = new Customer();

            customer1.FirstName = "customer1";
            context.Customers.Add(customer1);

            customer1.Orders.Add(order1);

            #region check

            order1.CheckAdded();
            customer1.CheckAdded();
            orderDetail1.CheckAdded();

            context.Changes.CheckCount(3)
            .CheckFound(order1)
            .CheckFound(customer1)
            .CheckFound(orderDetail1);

            #endregion check

            // save changes
            context.Execute(save);

            #region check

            context.Changes.CheckCount(0);

            order1.CheckUnchanged();
            customer1.CheckUnchanged();
            orderDetail1.CheckUnchanged();

            customer1.Orders.CheckCount(1).CheckItem(0, order1);

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

            orderDetail1.Order.Check(order1);
            orderDetail1.OrderID.Check(order1.ID);

            order1.GetChangedProperties().CheckCount(0);
            customer1.GetChangedProperties().CheckCount(0);
            orderDetail1.GetChangedProperties().CheckCount(0);

            #endregion check

            // Add shipment1
            Shipment shipment1 = new Shipment();
            context.Shipments.Add(shipment1);

            // Add order2
            Order order2 = new Order();
            context.Orders.Add(order2);

            // Modify orderDetail1
            orderDetail1.Order = order2;

            // Add orderDetail2
            OrderDetail orderDetail2 = new OrderDetail();
            orderDetail2.Order = order2;
            context.OrderDetails.Add(orderDetail2);

            // Modify order1
            order1.Customer = null;

            // Add customer2
            Customer customer2 = new Customer();
            customer2.FirstName = "customer2";
            context.Customers.Add(customer2);

            // Modify customer1
            customer1.SecondPartner = customer2;

            // Modify order1
            order1.Customer = customer2;

            shipment1.Order = order2;

            // Add customer3
            Customer customer3 = new Customer();
            customer3.FirstName = "customer3";
            context.Customers.Add(customer3);

            customer2.FirstPartner = customer3;

            // Delete customer1
            context.Customers.Remove(customer1);

            order2.Customer = customer2;

            // Modify order1
            order1.Customer = customer3;

            #region check

            order2.CheckAdded();
            shipment1.CheckAdded();
            customer2.CheckAdded();
            customer3.CheckAdded();
            orderDetail2.CheckAdded();

            customer1.CheckDeleted();

            order1.CheckModified();
            order1.Customer.Check(customer3);
            order1.CustomerID.Check(customer3.ID);
            order1.GetChangedProperties().CheckCount(1).CheckFound <Order>(o => o.CustomerID);

            orderDetail1.CheckModified();
            orderDetail1.Order.Check(order2);
            orderDetail1.OrderID.Check(order2.ID);
            orderDetail1.GetChangedProperties().CheckCount(1).CheckFound <OrderDetail>(od => od.OrderID);

            context.Changes.CheckCount(8)
            .CheckFound(order1)
            .CheckFound(order2)
            .CheckFound(shipment1)
            .CheckFound(customer1)
            .CheckFound(customer2)
            .CheckFound(customer3)
            .CheckFound(orderDetail1)
            .CheckFound(orderDetail2);

            #endregion check

            #region delete order1

            async Task delete()
            {
                EntityDatabaseConnection connection = GetConnection();

                await connection.OpenAsync();

                connection.BeginTransaction(IsolationLevel.Snapshot);

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

                                       delete Orders
                                       where ID = @OrderID;

                                       waitfor delay '00:00:00.5'

                                      ")
                .ExecuteAsync()
                .ContinueWith(t =>
                {
                    if (!t.IsFaulted)
                    {
                        throw new Exception("Expected Exception, Was Not Thrown");
                    }

                    connection.RollbackTransaction();
                    connection.Close();
                });
            }

            Task task = delete();

            #endregion delete order1

            // save changes
            context.Execute(save);

            task.Wait();

            #region check

            order1.CheckUnchanged();
            order2.CheckUnchanged();
            shipment1.CheckUnchanged();
            customer2.CheckUnchanged();
            customer3.CheckUnchanged();
            orderDetail1.CheckUnchanged();
            orderDetail2.CheckUnchanged();

            customer1.CheckDetached();

            order1.Customer.Check(customer3);
            order1.CustomerID.Check(customer3.ID);
            order1.GetChangedProperties().CheckCount(0);
            order1.OrderDetails.CheckCount(0);
            order1.Shipment.CheckItem(null);

            order2.Customer.Check(customer2);
            order2.CustomerID.Check(customer2.ID);
            order2.GetChangedProperties().CheckCount(0);
            order2.OrderDetails.CheckCount(2).CheckItem(0, orderDetail1).CheckItem(1, orderDetail2);
            order2.Shipment.CheckItem(shipment1);

            shipment1.Order.Check(order2);
            shipment1.OrderID.Check(order2.ID);
            shipment1.GetChangedProperties().CheckCount(0);

            customer2.FirstPartner.Check(customer3);
            customer2.FirstPartnerID.Check(customer3.ID);
            customer2.SecondPartner.Check(null);
            customer2.SecondPartnerID.Check(null);
            customer2.GetChangedProperties().CheckCount(0);
            customer2.Orders.CheckCount(1).CheckItem(0, order2);
            customer2.FirstPartners.CheckCount(0);
            customer2.SecondPartners.CheckCount(0);

            customer3.FirstPartner.Check(null);
            customer3.FirstPartnerID.Check(null);
            customer3.SecondPartner.Check(null);
            customer3.SecondPartnerID.Check(null);
            customer2.GetChangedProperties().CheckCount(0);
            customer3.Orders.CheckCount(1).CheckItem(0, order1);
            customer3.FirstPartners.CheckCount(1).CheckItem(0, customer2);
            customer3.SecondPartners.CheckCount(0);

            orderDetail1.Order.Check(order2);
            orderDetail1.OrderID.Check(order2.ID);
            orderDetail1.GetChangedProperties().CheckCount(0);

            orderDetail2.Order.Check(order2);
            orderDetail2.OrderID.Check(order2.ID);
            orderDetail2.GetChangedProperties().CheckCount(0);

            context.Changes.CheckCount(0);

            #endregion check
        }