// Obsolete as of 2007.1.1021.0
        //protected override void CreateExtendedProperties(esColumnMetadataCollection extendedProps)
        //{
        //    if (null == extendedProps["NewId"])
        //    {
        //        esColumnMetadata col = new esColumnMetadata("NewId", this.Meta.Columns.Count + 1, Type.GetType("System.Int32"));
        //        col.PropertyName = "NewId";
        //        extendedProps.Add(col);
        //    }

        //    if (null == extendedProps["OrderIndex"])
        //    {
        //        esColumnMetadata col = new esColumnMetadata("OrderIndex", this.Meta.Columns.Count + 1, Type.GetType("System.Int32"));
        //        col.PropertyName = "OrderIndex";
        //        extendedProps.Add(col);
        //    }
        //}

		//public void AddOrderIndexColumn()
		//{
		//    if (this.Table != null && this.Table.Columns.Contains("OrderIndex") == false)
		//    {
		//        this.Table.Columns.Add("OrderIndex", typeof(Int16));
		//    }
		//}

		//public DataTable GetTable()
		//{
		//    return this.Table;
		//}

        public static void CustomForEach()
        {
            AggregateTestCollection coll = new AggregateTestCollection();
            coll.LoadAll();

            foreach (AggregateTest entity in coll)
            {
                entity.LastName = "CustomForEach";
            }
        }
        // Obsolete as of 2007.1.1021.0
        //protected override void CreateExtendedProperties(esColumnMetadataCollection extendedProps)
        //{
        //    if (null == extendedProps["NewId"])
        //    {
        //        esColumnMetadata col = new esColumnMetadata("NewId", this.Meta.Columns.Count + 1, Type.GetType("System.Int32"));
        //        col.PropertyName = "NewId";
        //        extendedProps.Add(col);
        //    }

        //    if (null == extendedProps["OrderIndex"])
        //    {
        //        esColumnMetadata col = new esColumnMetadata("OrderIndex", this.Meta.Columns.Count + 1, Type.GetType("System.Int32"));
        //        col.PropertyName = "OrderIndex";
        //        extendedProps.Add(col);
        //    }
        //}

        //public void AddOrderIndexColumn()
        //{
        //    if (this.Table != null && this.Table.Columns.Contains("OrderIndex") == false)
        //    {
        //        this.Table.Columns.Add("OrderIndex", typeof(Int16));
        //    }
        //}

        //public DataTable GetTable()
        //{
        //    return this.Table;
        //}

        public static void CustomForEach()
        {
            AggregateTestCollection coll = new AggregateTestCollection();

            coll.LoadAll();

            foreach (AggregateTest entity in coll)
            {
                entity.LastName = "CustomForEach";
            }
        }
		public void EmptyQueryReturnsSelectAll()
		{
            aggTestColl = new AggregateTestCollection();
            Assert.IsTrue(aggTestColl.LoadAll());
            Assert.AreEqual(30, aggTestColl.Count, "LoadAll");

            aggTestColl = new AggregateTestCollection();
            Assert.IsTrue(aggTestColl.Query.Load());
            Assert.AreEqual(30, aggTestColl.Count, "Query.Load");

            aggTestColl = new AggregateTestCollection();
            aggTestColl.Query.LoadDataTable();
			Assert.AreEqual(0, aggTestColl.Count, "Query.LoadDataTable");
		}
        public void TestIdentityInsert()
        {
            AggregateTestCollection coll = new AggregateTestCollection();

            coll.LoadAll();

            StringBuilder sql = new StringBuilder();

            sql.AppendLine("SET IDENTITY_INSERT [AggregateTest] ON");
            sql.AppendLine("SET NOCOUNT ON");

            for (int i = 1; i < coll.Count; i++)
            {
                sql.AppendLine("INSERT INTO [AggregateTest]([ID], [LastName])");
                sql.AppendLine("VALUES(" + i.ToString() + ", '" + coll[i].LastName + "')");
            }

            sql.AppendLine("SET IDENTITY_INSERT [AggregateTest] OFF");

            //esUtility u = new esUtility();
            //u.ExecuteNonQuery(esQueryType.Text, sql.ToString());
        }
        public void TestIdentityInsert()
        {
            AggregateTestCollection coll = new AggregateTestCollection();
            coll.LoadAll();

            StringBuilder sql = new StringBuilder();
            sql.AppendLine("SET IDENTITY_INSERT [AggregateTest] ON");
            sql.AppendLine("SET NOCOUNT ON");

            for (int i = 1; i < coll.Count; i++)
            {
                sql.AppendLine("INSERT INTO [AggregateTest]([ID], [LastName])");
                sql.AppendLine("VALUES(" + i.ToString() + ", '" + coll[ i ].LastName + "')");
            }

            sql.AppendLine("SET IDENTITY_INSERT [AggregateTest] OFF");

            //esUtility u = new esUtility();
            //u.ExecuteNonQuery(esQueryType.Text, sql.ToString());
        }
        public void TestCommandTimeoutConfig()
        {
            switch (aggTestColl.es.Connection.ProviderSignature.DataProviderName)
            {
                case "EntitySpaces.SqlClientProvider":
                    // Collection
                    aggTestColl = new AggregateTestCollection();
                    Assert.AreEqual(39, aggTestColl.es.Connection.CommandTimeout);
                    Assert.IsTrue(aggTestColl.Query.Load(), "Query.Load");
                    aggTestColl = new AggregateTestCollection();
                    Assert.AreEqual(39, aggTestColl.es.Connection.CommandTimeout);
                    Assert.IsTrue(aggTestColl.LoadAll(), "LoadAll");

                    // Entity
                    aggTest = new AggregateTest();
                    Assert.AreEqual(39, aggTest.es.Connection.CommandTimeout);
                    aggTest.Query.es.Top = 1;
                    Assert.IsTrue(aggTest.Query.Load(), "Query.Load");
                    int aggKey = aggTest.Id.Value;
                    aggTest = new AggregateTest();
                    Assert.AreEqual(39, aggTest.es.Connection.CommandTimeout);
                    Assert.IsTrue(aggTest.LoadByPrimaryKey(aggKey), "LoadByPK");
                    break;

                default:
                    Assert.Ignore("tested for SQL Server only");
                    break;
            }
        }
        public void TestCommandTimeout()
        {
            switch (aggTestColl.es.Connection.ProviderSignature.DataProviderName)
            {
                case "EntitySpaces.SqlClientProvider":
                    aggTestColl = new AggregateTestCollection();
                    aggTestColl.es.Connection.CommandTimeout = 49;
                    Assert.IsTrue(aggTestColl.Query.Load(), "Query.Load");
                    Assert.AreEqual(49, aggTestColl.es.Connection.CommandTimeout);
                    aggTestColl = new AggregateTestCollection();
                    Assert.IsTrue(aggTestColl.LoadAll(), "LoadAll");
                    Assert.AreEqual(39, aggTestColl.es.Connection.CommandTimeout);
                    break;

                default:
                    Assert.Ignore("SQL Server only");
                    break;
            }
        }
		public void CollectionLoadAllFalse()
		{
			AggregateTestCollection testColl = new AggregateTestCollection();

            try
            {
                testColl.LoadAll();
                testColl.MarkAllAsDeleted();
                testColl.Save();

                testColl = new AggregateTestCollection();
                Assert.IsFalse(testColl.LoadAll());
            }
            finally
            {
                UnitTestBase.RefreshDatabase();
            }
		}
		public void NoAddNew()
		{
            int aggTestId = -1;

            try
            {
                using (esTransactionScope scope = new esTransactionScope())
                {
                    aggTest = new AggregateTest();
                    aggTest.Age = 30;
                    aggTest.Save();
                    aggTestId = aggTest.Id.Value;

                    aggTest = new AggregateTest();
                    Assert.IsTrue(aggTest.LoadByPrimaryKey(aggTestId));
                    Assert.AreEqual(30, aggTest.Age.Value);
                }
            }
            finally
            {
                // Clean up
                aggTest = new AggregateTest();
                if (aggTest.LoadByPrimaryKey(aggTestId))
                {
                    aggTest.MarkAsDeleted();
                    aggTest.Save();
                }

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
            }
		}
        public void RejectChangesCollection()
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                aggTest = aggTestColl.AddNew();
                aggTestColl.Save();
                int aggTestId = aggTest.Id.Value;

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(31, aggTestColl.Count);

                aggTest = aggTestColl.FindByPrimaryKey(aggTestId);
                aggTest.MarkAsDeleted();
                aggTestColl.AddNew();
                aggTestColl.RejectChanges();
                aggTestColl.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(31, aggTestColl.Count);

                // Cleanup
                aggTest = new AggregateTest();
                Assert.IsTrue(aggTest.LoadByPrimaryKey(aggTestId));
                aggTest.MarkAsDeleted();
                aggTest.Save();
            }
        }
        public void UpdateOrInsertEntityCustom()
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                // The PK does not exist, so insert
                AggregateTest entity = new AggregateTest();
                entity.SaveUpdateOrInsert(-1, "IsInsert");
                int aggTestId = entity.Id.Value;

                entity = new AggregateTest();
                Assert.IsTrue(entity.LoadByPrimaryKey(aggTestId));
                Assert.AreEqual("IsInsert", entity.LastName);

                // The PK does exist, so update
                entity = new AggregateTest();
                entity.SaveUpdateOrInsert(aggTestId, "IsUpdate");

                entity = new AggregateTest();
                Assert.IsTrue(entity.LoadByPrimaryKey(aggTestId));
                Assert.AreEqual("IsUpdate", entity.LastName);

                // Clean up
                entity.MarkAsDeleted();
                entity.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
            }
        }
        public void UpdateOrInsertEntity()
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                // The PK does not exist, so insert
                aggTest = new AggregateTest();
                Assert.IsFalse(aggTest.LoadByPrimaryKey(0));
                aggTest.LastName = "IsInsert";
                aggTest.Save();
                int aggTestId = aggTest.Id.Value;

                // The PK does exist, so update
                aggTest = new AggregateTest();
                Assert.IsTrue(aggTest.LoadByPrimaryKey(aggTestId));
                Assert.AreEqual("IsInsert", aggTest.LastName);
                aggTest.LastName = "IsUpdate";
                aggTest.Save();

                aggTest = new AggregateTest();
                Assert.IsTrue(aggTest.LoadByPrimaryKey(aggTestId));
                Assert.AreEqual("IsUpdate", aggTest.LastName);

                // Clean up
                aggTest.MarkAsDeleted();
                aggTest.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
            }
        }
		public void RetrieveAutoIncrementingKey()
		{
            using (esTransactionScope scope = new esTransactionScope())
            {
                aggTest = aggTestColl.AddNew();
                aggTestColl.Save();
                int aggTestId = aggTest.Id.Value;

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(31, aggTestColl.Count);

                aggTest = new AggregateTest();
                Assert.IsTrue(aggTest.LoadByPrimaryKey(aggTestId));
                aggTest.MarkAsDeleted();
                aggTest.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
            }
		}
        public void EmptyCollection()
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                aggTestColl = new AggregateTestCollection();
                aggTestColl.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
            }
        }
        public void EmptyEntity()
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                aggTest = new AggregateTest();
                aggTest.Save();
                int aggTestId = aggTest.Id.Value;

                aggTest = new AggregateTest();
                Assert.IsTrue(aggTest.LoadByPrimaryKey(aggTestId));

                // Clean up
                aggTest.MarkAsDeleted();
                aggTest.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
            }
        }
		public static void RefreshDatabase(string connectionName)
		{
            AggregateTestCollection aggTestColl = new AggregateTestCollection();
			if (connectionName.Length != 0)
			{
				aggTestColl.es.Connection.Name = connectionName;
			}

			aggTestColl.LoadAll();
            aggTestColl.MarkAllAsDeleted();
			aggTestColl.Save();

			aggTestColl = new AggregateTestCollection();
			AggregateTest aggTest = new AggregateTest();
			if (connectionName.Length != 0)
			{
				aggTestColl.es.Connection.Name = connectionName;
				aggTest.es.Connection.Name = connectionName;
			}

            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "David";
            aggTest.str().LastName = "Doe";
            aggTest.str().Age = "16";
            aggTest.str().HireDate = "2000-02-16 05:59:31";
            aggTest.str().Salary = "34.71";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "1";
            aggTest.str().FirstName = "Sarah";
            aggTest.str().LastName = "McDonald";
            aggTest.str().Age = "28";
            aggTest.str().HireDate = "1999-03-25 00:00:00";
            aggTest.str().Salary = "11.06";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "David";
            aggTest.str().LastName = "Vincent";
            aggTest.str().Age = "43";
            aggTest.str().HireDate = "2000-10-17 00:00:00";
            aggTest.str().Salary = "10.27";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "2";
            aggTest.str().FirstName = "Fred";
            aggTest.str().LastName = "Smith";
            aggTest.str().Age = "15";
            aggTest.str().HireDate = "1999-03-15 00:00:00";
            aggTest.str().Salary = "15.15";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "Sally";
            aggTest.str().LastName = "Johnson";
            aggTest.str().Age = "30";
            aggTest.str().HireDate = "2000-10-07 00:00:00";
            aggTest.str().Salary = "14.36";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "5";
            aggTest.str().FirstName = "Jane";
            aggTest.str().LastName = "Rapaport";
            aggTest.str().Age = "44";
            aggTest.str().HireDate = "2002-05-02 00:00:00";
            aggTest.str().Salary = "13.56";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "4";
            aggTest.str().FirstName = "Paul";
            aggTest.str().LastName = "Gellar";
            aggTest.str().Age = "16";
            aggTest.str().HireDate = "2000-09-27 00:00:00";
            aggTest.str().Salary = "18.44";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "2";
            aggTest.str().FirstName = "John";
            aggTest.str().LastName = "Jones";
            aggTest.str().Age = "31";
            aggTest.str().HireDate = "2002-04-22 00:00:00";
            aggTest.str().Salary = "17.65";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "Michelle";
            aggTest.str().LastName = "Johnson";
            aggTest.str().Age = "45";
            aggTest.str().HireDate = "2003-11-14 00:00:00";
            aggTest.str().Salary = "16.86";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "2";
            aggTest.str().FirstName = "David";
            aggTest.str().LastName = "Costner";
            aggTest.str().Age = "17";
            aggTest.str().HireDate = "2002-04-11 00:00:00";
            aggTest.str().Salary = "21.74";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "4";
            aggTest.str().FirstName = "William";
            aggTest.str().LastName = "Gellar";
            aggTest.str().Age = "32";
            aggTest.str().HireDate = "2003-11-04 00:00:00";
            aggTest.str().Salary = "20.94";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "Sally";
            aggTest.str().LastName = "Rapaport";
            aggTest.str().Age = "39";
            aggTest.str().HireDate = "2002-04-01 00:00:00";
            aggTest.str().Salary = "25.82";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "5";
            aggTest.str().FirstName = "Jane";
            aggTest.str().LastName = "Vincent";
            aggTest.str().Age = "18";
            aggTest.str().HireDate = "2003-10-25 00:00:00";
            aggTest.str().Salary = "25.03";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "2";
            aggTest.str().FirstName = "Fred";
            aggTest.str().LastName = "Costner";
            aggTest.str().Age = "33";
            aggTest.str().HireDate = "1998-05-20 00:00:00";
            aggTest.str().Salary = "24.24";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "1";
            aggTest.str().FirstName = "John";
            aggTest.str().LastName = "Johnson";
            aggTest.str().Age = "40";
            aggTest.str().HireDate = "2003-10-15 00:00:00";
            aggTest.str().Salary = "29.12";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "Michelle";
            aggTest.str().LastName = "Rapaport";
            aggTest.str().Age = "19";
            aggTest.str().HireDate = "1998-05-10 00:00:00";
            aggTest.str().Salary = "28.32";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "4";
            aggTest.str().FirstName = "Sarah";
            aggTest.str().LastName = "Doe";
            aggTest.str().Age = "34";
            aggTest.str().HireDate = "1999-12-03 00:00:00";
            aggTest.str().Salary = "27.53";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "4";
            aggTest.str().FirstName = "William";
            aggTest.str().LastName = "Jones";
            aggTest.str().Age = "41";
            aggTest.str().HireDate = "1998-04-30 00:00:00";
            aggTest.str().Salary = "32.41";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "1";
            aggTest.str().FirstName = "Sarah";
            aggTest.str().LastName = "McDonald";
            aggTest.str().Age = "21";
            aggTest.str().HireDate = "1999-11-23 00:00:00";
            aggTest.str().Salary = "31.62";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "4";
            aggTest.str().FirstName = "Jane";
            aggTest.str().LastName = "Costner";
            aggTest.str().Age = "28";
            aggTest.str().HireDate = "1998-04-20 00:00:00";
            aggTest.str().Salary = "36.50";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "2";
            aggTest.str().FirstName = "Fred";
            aggTest.str().LastName = "Douglas";
            aggTest.str().Age = "42";
            aggTest.str().HireDate = "1999-11-13 00:00:00";
            aggTest.str().Salary = "35.71";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "Sarah";
            aggTest.str().LastName = "Jones";
            aggTest.str().Age = "22";
            aggTest.str().HireDate = "2001-06-07 00:00:00";
            aggTest.str().Salary = "34.91";
            aggTest.str().IsActive = "false";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "3";
            aggTest.str().FirstName = "Michelle";
            aggTest.str().LastName = "Doe";
            aggTest.str().Age = "29";
            aggTest.str().HireDate = "1999-11-03 00:00:00";
            aggTest.str().Salary = "39.79";
            aggTest.str().IsActive = "true";
            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "4";
            aggTest.str().FirstName = "Paul";
            aggTest.str().LastName = "Costner";
            aggTest.str().Age = "43";
            aggTest.str().HireDate = "2001-05-28 00:00:00";
            aggTest.str().Salary = "39.00";
            aggTest.str().IsActive = "true";

            aggTest = aggTestColl.AddNew();
            aggTest.str().DepartmentID = "0";
            aggTest.str().FirstName = "";
            aggTest.str().LastName = "";
            aggTest.str().Age = "0";
            aggTest.str().Salary = "0";

            aggTest = aggTestColl.AddNew();
            aggTest = aggTestColl.AddNew();
            aggTest = aggTestColl.AddNew();
            aggTest = aggTestColl.AddNew();
            aggTest = aggTestColl.AddNew();

            aggTestColl.Save();

        }
        public void FilteredDeleteAll()
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                AggregateTestCollection aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(30, aggTestColl.Count);
                aggTestColl.Filter = aggTestColl.AsQueryable().Where(f => f.LastName == "Doe");
                Assert.AreEqual(3, aggTestColl.Count);

                aggTestColl.MarkAllAsDeleted();

                aggTestColl.Filter = null;
                Assert.AreEqual(27, aggTestColl.Count);

                aggTestColl.Save();

                aggTestColl = new AggregateTestCollection();
                aggTestColl.LoadAll();
                Assert.AreEqual(27, aggTestColl.Count);
            }
        }