예제 #1
0
        public void Update(NorthwindSystem.DataModels.HumanResources.Region region, List <NorthwindSystem.DataModels.HumanResources.Territory> territories)
        {
            if (region == null)
            {
                throw new ArgumentNullException("region", "region is null.");
            }
            if (territories == null)
            {
                throw new ArgumentNullException("territories", "territories is null.");
            }

            using (var dbContext = new HR.NorthwindHumanResources())
            {
                foreach (var item in territories)
                {
                    var found = dbContext.Territories.Find(item.TerritoryID);
                    if (found != null)
                    {
                        /* NOTE:
                         *  Pre-process the Territory IDs to see if they should be "synced" with the name/description.
                         *  This will be the case if, in the original, the ID was the same as the description
                         */
                        string foundTerritoryID          = found.TerritoryID;
                        string foundTerritoryDescription = found.TerritoryDescription.Trim(); // HACK: Turns out, the column is nchar(50), not an nvarchar....
                        string itemTerritoryID           = item.TerritoryID;
                        string itemTerritoryDescription  = item.TerritoryDescription.Trim();
                        if (foundTerritoryID.Equals(foundTerritoryDescription) &&
                            !itemTerritoryID.Equals(itemTerritoryDescription))
                        {
                            item.TerritoryID = itemTerritoryDescription;
                            dbContext.Territories.Remove(found); // Because the PK has changed...
                            dbContext.Territories.Add(item);     // Because the PK has changed...
                        }
                    }
                }

                dbContext.Entry(region).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
            }
        }
예제 #2
0
        public int Add(HR.Region region)
        {
            if (region == null)
            {
                throw new ArgumentNullException("region", "region is null.");
            }
            using (var dbContext = new HR.NorthwindHumanResources())
            {
                /* NOTE:
                 *  The TerritoryID column in Territories is a string - nvarchar(20) - rather than an integer.
                 *  The existing data in Northwind Traders uses the zip code of the city/town as the TerritoryID.
                 *  This sample just "simplifies" and assigns the territory description as the ID, since we're
                 *  in Canada and we aren't using a single zip or postal code.
                 */
                foreach (var territory in region.Territories)
                {
                    if (string.IsNullOrEmpty(territory.TerritoryID))
                    {
                        territory.TerritoryID = territory.TerritoryDescription;
                    }
                }

                /* NOTE:
                 *  The RegionID column in Regions is an integer, but it is not an IDENTITY column.
                 *  As such, we're simply going to get the next highest ID available.
                 */
                if (region.RegionID <= 0)
                {
                    region.RegionID = dbContext.Regions.Max(item => item.RegionID) + 1;
                }

                dbContext.Regions.Add(region);

                dbContext.SaveChanges();

                return(region.RegionID);
            }
        }
예제 #3
0
        public int Add(HR.Region region)
        {
            if (region == null)
                throw new ArgumentNullException("region", "region is null.");
            using (var dbContext = new HR.NorthwindHumanResources())
            {
                /* NOTE:
                 *  The TerritoryID column in Territories is a string - nvarchar(20) - rather than an integer.
                 *  The existing data in Northwind Traders uses the zip code of the city/town as the TerritoryID.
                 *  This sample just "simplifies" and assigns the territory description as the ID, since we're
                 *  in Canada and we aren't using a single zip or postal code.
                 */
                foreach (var territory in region.Territories)
                    if (string.IsNullOrEmpty(territory.TerritoryID))
                        territory.TerritoryID = territory.TerritoryDescription;

                /* NOTE:
                 *  The RegionID column in Regions is an integer, but it is not an IDENTITY column.
                 *  As such, we're simply going to get the next highest ID available.
                 */
                if(region.RegionID <= 0)
                    region.RegionID = dbContext.Regions.Max(item => item.RegionID) + 1;

                dbContext.Regions.Add(region);

                dbContext.SaveChanges();

                return region.RegionID;
            }
        }
예제 #4
0
        public void Update(NorthwindSystem.DataModels.HumanResources.Region region, List<NorthwindSystem.DataModels.HumanResources.Territory> territories)
        {
            if (region == null)
                throw new ArgumentNullException("region", "region is null.");
            if (territories == null)
                throw new ArgumentNullException("territories", "territories is null.");

            using (var dbContext = new HR.NorthwindHumanResources())
            {
                foreach (var item in territories)
                {
                    var found = dbContext.Territories.Find(item.TerritoryID);
                    if (found != null)
                    {
                        /* NOTE:
                         *  Pre-process the Territory IDs to see if they should be "synced" with the name/description.
                         *  This will be the case if, in the original, the ID was the same as the description
                         */
                        string foundTerritoryID = found.TerritoryID;
                        string foundTerritoryDescription = found.TerritoryDescription.Trim(); // HACK: Turns out, the column is nchar(50), not an nvarchar....
                        string itemTerritoryID = item.TerritoryID;
                        string itemTerritoryDescription = item.TerritoryDescription.Trim();
                        if (foundTerritoryID.Equals(foundTerritoryDescription) &&
                            !itemTerritoryID.Equals(itemTerritoryDescription))
                        {
                            item.TerritoryID = itemTerritoryDescription;
                            dbContext.Territories.Remove(found); // Because the PK has changed...
                            dbContext.Territories.Add(item); // Because the PK has changed...
                        }
                    }
                }

                dbContext.Entry(region).State = System.Data.EntityState.Modified;
                dbContext.SaveChanges();
            }
        }