示例#1
0
        public void PutSetDirectReports(int id, [FromBody] EmployeeDirectReports item)
        {
            // Attention - Employee set direct-reports - command pattern

            // Ensure that an "item" is in the entity body
            if (item == null)
            {
                return;
            }

            // Ensure that the id value in the URI matches the id value in the entity body
            if (id != item.EmployeeId)
            {
                return;
            }

            // Ensure that we can use the incoming data
            if (ModelState.IsValid)
            {
                // Attempt to update the item
                m.EmployeeSetDirectReports(item);
            }
            else
            {
                return;
            }
        }
示例#2
0
        public EmployeeWithDetails EmployeeSetDirectReports(EmployeeDirectReports newItem)
        {
            // Attention - Manager - Employee - command to update the self-referencing to-many association

            // Attempt to fetch the object

            // When editing an object with a to-many collection,
            // and you wish to edit the collection,
            // MUST fetch its associated collection
            var o = ds.Employees.Include("DirectReports").Include("ReportsTo")
                    .SingleOrDefault(e => e.EmployeeId == newItem.EmployeeId);

            if (o == null)
            {
                // Problem - object was not found, so return
                return(null);
            }
            else
            {
                // Update the object with the incoming values

                // First, clear out the existing collection
                // "DirectReports" is the badly-named to-many collection property
                o.DirectReports.Clear();

                // Then, go through the incoming items
                // For each one, add to the fetched object's collection
                foreach (var item in newItem.EmployeeIds)
                {
                    var a = ds.Employees.Find(item);
                    if (a != null)
                    {
                        o.DirectReports.Add(a);
                    }
                }
                // Save changes
                ds.SaveChanges();

                return(mapper.Map <EmployeeWithDetails>(o));
            }
        }