public int Add(Region region)
        {
            // TODO: Add Unit Test
            if (region == null)
                throw new ArgumentNullException("region", "region is null.");
            using (var dbContext = new NWContext())
            {
                /* 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;
            }
        }
Пример #2
0
 public int AddShipper(Shipper info)
 {
     using (var context = new NWContext())
     {
         context.Shippers.Add(info);
         context.SaveChanges();
         return info.ShipperID;
     }
 }
Пример #3
0
 public int AddProduct(Product info)
 {
     using (var context = new NWContext())
     {
         context.Products.Add(info);
         context.SaveChanges();
         return info.ProductID;
     }
 }
Пример #4
0
 public void DeleteProduct(Product product)
 {
     using (var context = new NWContext())
     {
         var found = context.Products.Find(product.ProductID);
         if (found != null)
         {
             context.Products.Remove(found);
             context.SaveChanges();
         }
     }
 }
Пример #5
0
        public void DeleteShipper(Shipper shipper)
        {
            using (var context = new NWContext())
            {

                //BEST practice. find shipper first, then delete
                var found = context.Shippers.Find(shipper.ShipperID);
                if (found != null)
                {
                    context.Shippers.Remove(found);
                    context.SaveChanges();
                }
            }
        }
Пример #6
0
        public void UpdateShipper(Shipper info)
        {
            //Note: see question and commentary on
            //http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-reocrd
            using (var context = new NWContext())
            {
                context.Shippers.Attach(info);
                context.Entry(info).State = EntityState.Modified;
                context.SaveChanges();

            }
        }
Пример #7
0
        public void UpdateProduct(Product info)
        {
            using (var context = new NWContext())
            {
                context.Products.Attach(info);
                context.Entry(info).State = EntityState.Modified;
                context.SaveChanges();

            }
        }
        public void Update(Region region, List<Territory> territories)
        {
            // TODO: Add Unit Test
            if (region == null)
                throw new ArgumentNullException("region", "region is null.");
            if (territories == null)
                throw new ArgumentNullException("territories", "territories is null.");

            using (var dbContext = new NWContext())
            {
                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 = EntityState.Modified;
                dbContext.SaveChanges();
            }
        }