// <fileinfo name="EmployeeTerritoriesCollection_Base.cs">
//      <copyright>
//          All rights reserved.
//      </copyright>
//      <remarks>
//          Do not change this source code manually. Changes to this file may 
//          cause incorrect behavior and will be lost if the code is regenerated.
//      </remarks>
//      <generator rewritefile="True" infourl="http://www.SharpPower.com">RapTier</generator>
// </fileinfo>

using System;
using System.Data;

namespace MyCompany.MyProject.Db
{
    /// <summary>
    /// The base class for <see cref="EmployeeTerritoriesCollection"/>. Provides methods 
    /// for common database table operations. 
    /// </summary>
    /// <remarks>
    /// Do not change this source code. Update the <see cref="EmployeeTerritoriesCollection"/>
    /// class if you need to add or change some functionality.
    /// </remarks>
    public abstract class EmployeeTerritoriesCollection_Base
    {
        // Constants
        public const string EmployeeIDColumnName = "EmployeeID";
        public const string TerritoryIDColumnName = "TerritoryID";

        // Instance fields
        private Northwind _db;

        /// <summary>
        /// Initializes a new instance of the <see cref="EmployeeTerritoriesCollection_Base"/> 
        /// class with the specified <see cref="Northwind"/>.
        /// </summary>
        /// <param name="db">The <see cref="Northwind"/> object.</param>
        public EmployeeTerritoriesCollection_Base(Northwind db)
        {
            _db = db;
        }

        /// <summary>
        /// Gets the database object that this table belongs to.
        /// </summary>
        /// <value>The <see cref="Northwind"/> object.</value>
        protected Northwind Database
        {
            get { return _db; }
        }

        /// <summary>
        /// Gets an array of all records from the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        public virtual EmployeeTerritoriesRow[] GetAll()
        {
            return MapRecords(CreateGetAllCommand());
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object that 
        /// includes all records from the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetAllAsDataTable()
        {
            return MapRecordsToDataTable(CreateGetAllCommand());
        }

        /// <summary>
        /// Creates and returns an <see cref="System.Data.IDbCommand"/> object that is used
        /// to retrieve all records from the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetAllCommand()
        {
            return CreateGetCommand(null, null);
        }

        /// <summary>
        /// Gets the first <see cref="EmployeeTerritoriesRow"/> objects that 
        /// match the search condition.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: 
        /// <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <returns>An instance of <see cref="EmployeeTerritoriesRow"/> or null reference 
        /// (Nothing in Visual Basic) if the object was not found.</returns>
        public EmployeeTerritoriesRow GetRow(string whereSql)
        {
            int totalRecordCount = -1;
            EmployeeTerritoriesRow[] rows = GetAsArray(whereSql, null, 0, 1, ref totalRecordCount);
            return 0 == rows.Length ? null : rows[0];
        }

        /// <summary>
        /// Gets an array of <see cref="EmployeeTerritoriesRow"/> objects that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: 
        /// <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: <c>"LastName ASC, FirstName ASC"</c>.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        public EmployeeTerritoriesRow[] GetAsArray(string whereSql, string orderBySql)
        {
            int totalRecordCount = -1;
            return GetAsArray(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
        }

        /// <summary>
        /// Gets an array of <see cref="EmployeeTerritoriesRow"/> objects that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example:
        /// <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: <c>"LastName ASC, FirstName ASC"</c>.</param>
        /// <param name="startIndex">The index of the first record to return.</param>
        /// <param name="length">The number of records to return.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        public virtual EmployeeTerritoriesRow[] GetAsArray(string whereSql, string orderBySql,
                            int startIndex, int length, ref int totalRecordCount)
        {
            using(IDataReader reader = _db.ExecuteReader(CreateGetCommand(whereSql, orderBySql)))
            {
                return MapRecords(reader, startIndex, length, ref totalRecordCount);
            }
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object filled with data that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: "FirstName='Smith' AND Zip=75038".</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: "LastName ASC, FirstName ASC".</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public DataTable GetAsDataTable(string whereSql, string orderBySql)
        {
            int totalRecordCount = -1;
            return GetAsDataTable(whereSql, orderBySql, 0, int.MaxValue, ref totalRecordCount);
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object filled with data that 
        /// match the search condition, in the the specified sort order.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: "FirstName='Smith' AND Zip=75038".</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: "LastName ASC, FirstName ASC".</param>
        /// <param name="startIndex">The index of the first record to return.</param>
        /// <param name="length">The number of records to return.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetAsDataTable(string whereSql, string orderBySql,
                            int startIndex, int length, ref int totalRecordCount)
        {
            using(IDataReader reader = _db.ExecuteReader(CreateGetCommand(whereSql, orderBySql)))
            {
                return MapRecordsToDataTable(reader, startIndex, length, ref totalRecordCount);
            }
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object for the specified search criteria.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. For example: "FirstName='Smith' AND Zip=75038".</param>
        /// <param name="orderBySql">The column name(s) followed by "ASC" (ascending) or "DESC" (descending).
        /// Columns are sorted in ascending order by default. For example: "LastName ASC, FirstName ASC".</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetCommand(string whereSql, string orderBySql)
        {
            string sql = "SELECT * FROM [dbo].[EmployeeTerritories]";
            if(null != whereSql && 0 < whereSql.Length)
                sql += " WHERE " + whereSql;
            if(null != orderBySql && 0 < orderBySql.Length)
                sql += " ORDER BY " + orderBySql;
            return _db.CreateCommand(sql);
        }

        /// <summary>
        /// Gets <see cref="EmployeeTerritoriesRow"/> by the primary key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>An instance of <see cref="EmployeeTerritoriesRow"/> or null reference 
        /// (Nothing in Visual Basic) if the object was not found.</returns>
        public virtual EmployeeTerritoriesRow GetByPrimaryKey(int employeeID, string territoryID)
        {
            string whereSql = "[EmployeeID]=" + _db.CreateSqlParameterName("EmployeeID") + " AND " +
                              "[TerritoryID]=" + _db.CreateSqlParameterName("TerritoryID");
            IDbCommand cmd = CreateGetCommand(whereSql, null);
            AddParameter(cmd, "EmployeeID", employeeID);
            AddParameter(cmd, "TerritoryID", territoryID);
            EmployeeTerritoriesRow[] tempArray = MapRecords(cmd);
            return 0 == tempArray.Length ? null : tempArray[0];
        }

        /// <summary>
        /// Gets an array of <see cref="EmployeeTerritoriesRow"/> objects 
        /// by the <c>FK_EmployeeTerritories_Employees</c> foreign key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        public virtual EmployeeTerritoriesRow[] GetByEmployeeID(int employeeID)
        {
            return MapRecords(CreateGetByEmployeeIDCommand(employeeID));
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object 
        /// by the <c>FK_EmployeeTerritories_Employees</c> foreign key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetByEmployeeIDAsDataTable(int employeeID)
        {
            return MapRecordsToDataTable(CreateGetByEmployeeIDCommand(employeeID));
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to 
        /// return records by the <c>FK_EmployeeTerritories_Employees</c> foreign key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetByEmployeeIDCommand(int employeeID)
        {
            string whereSql = "";
            whereSql += "[EmployeeID]=" + _db.CreateSqlParameterName("EmployeeID");

            IDbCommand cmd = CreateGetCommand(whereSql, null);
            AddParameter(cmd, "EmployeeID", employeeID);
            return cmd;
        }

        /// <summary>
        /// Gets an array of <see cref="EmployeeTerritoriesRow"/> objects 
        /// by the <c>FK_EmployeeTerritories_Territories</c> foreign key.
        /// </summary>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        public virtual EmployeeTerritoriesRow[] GetByTerritoryID(string territoryID)
        {
            return MapRecords(CreateGetByTerritoryIDCommand(territoryID));
        }

        /// <summary>
        /// Gets a <see cref="System.Data.DataTable"/> object 
        /// by the <c>FK_EmployeeTerritories_Territories</c> foreign key.
        /// </summary>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        public virtual DataTable GetByTerritoryIDAsDataTable(string territoryID)
        {
            return MapRecordsToDataTable(CreateGetByTerritoryIDCommand(territoryID));
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to 
        /// return records by the <c>FK_EmployeeTerritories_Territories</c> foreign key.
        /// </summary>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateGetByTerritoryIDCommand(string territoryID)
        {
            string whereSql = "";
            whereSql += "[TerritoryID]=" + _db.CreateSqlParameterName("TerritoryID");

            IDbCommand cmd = CreateGetCommand(whereSql, null);
            AddParameter(cmd, "TerritoryID", territoryID);
            return cmd;
        }

        /// <summary>
        /// Adds a new record into the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <param name="value">The <see cref="EmployeeTerritoriesRow"/> object to be inserted.</param>
        public virtual void Insert(EmployeeTerritoriesRow value)
        {
            string sqlStr = "INSERT INTO [dbo].[EmployeeTerritories] (" +
                "[EmployeeID], " +
                "[TerritoryID]" +
                ") VALUES (" +
                _db.CreateSqlParameterName("EmployeeID") + ", " +
                _db.CreateSqlParameterName("TerritoryID") + ")";
            IDbCommand cmd = _db.CreateCommand(sqlStr);
            AddParameter(cmd, "EmployeeID", value.EmployeeID);
            AddParameter(cmd, "TerritoryID", value.TerritoryID);
            cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Deletes the specified object from the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <param name="value">The <see cref="EmployeeTerritoriesRow"/> object to delete.</param>
        /// <returns>true if the record was deleted; otherwise, false.</returns>
        public bool Delete(EmployeeTerritoriesRow value)
        {
            return DeleteByPrimaryKey(value.EmployeeID, value.TerritoryID);
        }

        /// <summary>
        /// Deletes a record from the <c>EmployeeTerritories</c> table using
        /// the specified primary key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>true if the record was deleted; otherwise, false.</returns>
        public virtual bool DeleteByPrimaryKey(int employeeID, string territoryID)
        {
            string whereSql = "[EmployeeID]=" + _db.CreateSqlParameterName("EmployeeID") + " AND " +
                              "[TerritoryID]=" + _db.CreateSqlParameterName("TerritoryID");
            IDbCommand cmd = CreateDeleteCommand(whereSql);
            AddParameter(cmd, "EmployeeID", employeeID);
            AddParameter(cmd, "TerritoryID", territoryID);
            return 0 < cmd.ExecuteNonQuery();
        }

        /// <summary>
        /// Deletes records from the <c>EmployeeTerritories</c> table using the 
        /// <c>FK_EmployeeTerritories_Employees</c> foreign key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <returns>The number of records deleted from the table.</returns>
        public int DeleteByEmployeeID(int employeeID)
        {
            return CreateDeleteByEmployeeIDCommand(employeeID).ExecuteNonQuery();
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
        /// delete records using the <c>FK_EmployeeTerritories_Employees</c> foreign key.
        /// </summary>
        /// <param name="employeeID">The <c>EmployeeID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateDeleteByEmployeeIDCommand(int employeeID)
        {
            string whereSql = "";
            whereSql += "[EmployeeID]=" + _db.CreateSqlParameterName("EmployeeID");

            IDbCommand cmd = CreateDeleteCommand(whereSql);
            AddParameter(cmd, "EmployeeID", employeeID);
            return cmd;
        }

        /// <summary>
        /// Deletes records from the <c>EmployeeTerritories</c> table using the 
        /// <c>FK_EmployeeTerritories_Territories</c> foreign key.
        /// </summary>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>The number of records deleted from the table.</returns>
        public int DeleteByTerritoryID(string territoryID)
        {
            return CreateDeleteByTerritoryIDCommand(territoryID).ExecuteNonQuery();
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used to
        /// delete records using the <c>FK_EmployeeTerritories_Territories</c> foreign key.
        /// </summary>
        /// <param name="territoryID">The <c>TerritoryID</c> column value.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateDeleteByTerritoryIDCommand(string territoryID)
        {
            string whereSql = "";
            whereSql += "[TerritoryID]=" + _db.CreateSqlParameterName("TerritoryID");

            IDbCommand cmd = CreateDeleteCommand(whereSql);
            AddParameter(cmd, "TerritoryID", territoryID);
            return cmd;
        }

        /// <summary>
        /// Deletes <c>EmployeeTerritories</c> records that match the specified criteria.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. 
        /// For example: <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <returns>The number of deleted records.</returns>
        public int Delete(string whereSql)
        {
            return CreateDeleteCommand(whereSql).ExecuteNonQuery();
        }

        /// <summary>
        /// Creates an <see cref="System.Data.IDbCommand"/> object that can be used 
        /// to delete <c>EmployeeTerritories</c> records that match the specified criteria.
        /// </summary>
        /// <param name="whereSql">The SQL search condition. 
        /// For example: <c>"FirstName='Smith' AND Zip=75038"</c>.</param>
        /// <returns>A reference to the <see cref="System.Data.IDbCommand"/> object.</returns>
        protected virtual IDbCommand CreateDeleteCommand(string whereSql)
        {
            string sql = "DELETE FROM [dbo].[EmployeeTerritories]";
            if(null != whereSql && 0 < whereSql.Length)
                sql += " WHERE " + whereSql;
            return _db.CreateCommand(sql);
        }

        /// <summary>
        /// Deletes all records from the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <returns>The number of deleted records.</returns>
        public int DeleteAll()
        {
            return Delete("");
        }

        /// <summary>
        /// Reads data using the specified command and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="command">The <see cref="System.Data.IDbCommand"/> object.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        protected EmployeeTerritoriesRow[] MapRecords(IDbCommand command)
        {
            using(IDataReader reader = _db.ExecuteReader(command))
            {
                return MapRecords(reader);
            }
        }

        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        protected EmployeeTerritoriesRow[] MapRecords(IDataReader reader)
        {
            int totalRecordCount = -1;
            return MapRecords(reader, 0, int.MaxValue, ref totalRecordCount);
        }

        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// an array of mapped objects.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <param name="startIndex">The index of the first record to map.</param>
        /// <param name="length">The number of records to map.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>An array of <see cref="EmployeeTerritoriesRow"/> objects.</returns>
        protected virtual EmployeeTerritoriesRow[] MapRecords(IDataReader reader, 
                                        int startIndex, int length, ref int totalRecordCount)
        {
            if(0 > startIndex)
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "StartIndex cannot be less than zero.");
            if(0 > length)
                throw new ArgumentOutOfRangeException("length", length, "Length cannot be less than zero.");

            int employeeIDColumnIndex = reader.GetOrdinal("EmployeeID");
            int territoryIDColumnIndex = reader.GetOrdinal("TerritoryID");

            System.Collections.ArrayList recordList = new System.Collections.ArrayList();
            int ri = -startIndex;
            while(reader.Read())
            {
                ri++;
                if(ri > 0 && ri <= length)
                {
                    EmployeeTerritoriesRow record = new EmployeeTerritoriesRow();
                    recordList.Add(record);

                    record.EmployeeID = Convert.ToInt32(reader.GetValue(employeeIDColumnIndex));
                    record.TerritoryID = Convert.ToString(reader.GetValue(territoryIDColumnIndex));

                    if(ri == length && 0 != totalRecordCount)
                        break;
                }
            }

            totalRecordCount = 0 == totalRecordCount ? ri + startIndex : -1;
            return (EmployeeTerritoriesRow[])(recordList.ToArray(typeof(EmployeeTerritoriesRow)));
        }

        /// <summary>
        /// Reads data using the specified command and returns 
        /// a filled <see cref="System.Data.DataTable"/> object.
        /// </summary>
        /// <param name="command">The <see cref="System.Data.IDbCommand"/> object.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        protected DataTable MapRecordsToDataTable(IDbCommand command)
        {
            using(IDataReader reader = _db.ExecuteReader(command))
            {
                return MapRecordsToDataTable(reader);
            }
        }

        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// a filled <see cref="System.Data.DataTable"/> object.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        protected DataTable MapRecordsToDataTable(IDataReader reader)
        {
            int totalRecordCount = 0;
            return MapRecordsToDataTable(reader, 0, int.MaxValue, ref totalRecordCount);
        }
        
        /// <summary>
        /// Reads data from the provided data reader and returns 
        /// a filled <see cref="System.Data.DataTable"/> object.
        /// </summary>
        /// <param name="reader">The <see cref="System.Data.IDataReader"/> object to read data from the table.</param>
        /// <param name="startIndex">The index of the first record to read.</param>
        /// <param name="length">The number of records to read.</param>
        /// <param name="totalRecordCount">A reference parameter that returns the total number 
        /// of records in the reader object if 0 was passed into the method; otherwise it returns -1.</param>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        protected virtual DataTable MapRecordsToDataTable(IDataReader reader, 
                                        int startIndex, int length, ref int totalRecordCount)
        {
            if(0 > startIndex)
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "StartIndex cannot be less than zero.");
            if(0 > length)
                throw new ArgumentOutOfRangeException("length", length, "Length cannot be less than zero.");

            int columnCount = reader.FieldCount;
            int ri = -startIndex;
            
            DataTable dataTable = CreateDataTable();
            dataTable.BeginLoadData();
            object[] values = new object[columnCount];

            while(reader.Read())
            {
                ri++;
                if(ri > 0 && ri <= length)
                {
                    reader.GetValues(values);
                    dataTable.LoadDataRow(values, true);

                    if(ri == length && 0 != totalRecordCount)
                        break;
                }
            }
            dataTable.EndLoadData();

            totalRecordCount = 0 == totalRecordCount ? ri + startIndex : -1;
            return dataTable;
        }

        /// <summary>
        /// Converts <see cref="System.Data.DataRow"/> to <see cref="EmployeeTerritoriesRow"/>.
        /// </summary>
        /// <param name="row">The <see cref="System.Data.DataRow"/> object to be mapped.</param>
        /// <returns>A reference to the <see cref="EmployeeTerritoriesRow"/> object.</returns>
        protected virtual EmployeeTerritoriesRow MapRow(DataRow row)
        {
            EmployeeTerritoriesRow mappedObject = new EmployeeTerritoriesRow();
            DataTable dataTable = row.Table;
            DataColumn dataColumn;
            // Column "EmployeeID"
            dataColumn = dataTable.Columns["EmployeeID"];
            if(!row.IsNull(dataColumn))
                mappedObject.EmployeeID = (int)row[dataColumn];
            // Column "TerritoryID"
            dataColumn = dataTable.Columns["TerritoryID"];
            if(!row.IsNull(dataColumn))
                mappedObject.TerritoryID = (string)row[dataColumn];
            return mappedObject;
        }

        /// <summary>
        /// Creates a <see cref="System.Data.DataTable"/> object for 
        /// the <c>EmployeeTerritories</c> table.
        /// </summary>
        /// <returns>A reference to the <see cref="System.Data.DataTable"/> object.</returns>
        protected virtual DataTable CreateDataTable()
        {
            DataTable dataTable = new DataTable(