コード例 #1
0
        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();
            }
        }
コード例 #2
0
 public void UpdateShipper(Shipper info)
 {
     //NOTE: See question and commentary on
     //  http://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record
     using (var context = new NWContext())
     {
         context.Shippers.Attach(info);
         context.Entry(info).State = EntityState.Modified;
         context.SaveChanges();
     }
 }