public static void RemoveChild(EnterpriseTestEntities entities, int parentId, int childId) { if (entities == null) { throw new ArgumentNullException("entities"); } ResourceWindowsCategory componentToRemove = ResourceWindowsCategory.SelectById(entities, childId); EntityCollection <ResourceWindowsCategory> parentList = componentToRemove.Parents; int count = 0; int deleteIndex = 0; foreach (ResourceWindowsCategory parent in parentList) { if (parent.CategoryId == parentId) { deleteIndex = count; } count++; } componentToRemove.Parents.Remove(componentToRemove.Parents.ElementAt(deleteIndex)); if (componentToRemove.Parents.Count < 1) { entities.DeleteObject(componentToRemove); entities.SaveChanges(); } }
public static List <ResourceWindowsCategory> SelectByParent(EnterpriseTestEntities entities, int parentId) { if (entities == null) { throw new ArgumentNullException("entities"); } ResourceWindowsCategory parent = entities.ResourceWindowsCategories.Where(p => p.CategoryId == parentId).FirstOrDefault(); if (parent != null) { return(parent.Children.OrderBy(x => x.CategoryType).ToList()); } return(null); }
/// <summary> /// Returns the parent ResourceWindowsCategory for the passed-in ResourceWindowsCategory item. /// </summary> /// <param name="entities"></param> /// <param name="name"></param> /// <param name="item"></param> /// <returns></returns> private static ResourceWindowsCategory GetParent(EnterpriseTestEntities entities, string name, ResourceWindowsCategory item, string categoryType) { //Try to find the parent in the item's Parent collection ResourceWindowsCategory result = item.Parents.Where(p => p.Name.Equals(name, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); if (result == null) { //Not in the item's parent collection. Try to find the parent in the table result = Select(entities, categoryType, name).FirstOrDefault(); } if (result == null) { //Not in the item's parent collection or the database. Create a new item result = ResourceWindowsCategory.CreateResourceWindowsCategory(NextId(entities), name, categoryType); } return(result); }
private static int Exists(EnterpriseTestEntities entities, string name, string categoryType) { int doesNotExist = -1; if (entities == null) { throw new ArgumentNullException("entities"); } try { ResourceWindowsCategory component = ResourceWindowsCategory.SelectByName(entities, name, categoryType); return(component.CategoryId); } catch { return(doesNotExist); } }
public static int AddResource(EnterpriseTestEntities entities, string name, string categoryType) { int doesNotExist = -1; int id = ResourceWindowsCategory.Exists(entities, name, categoryType); if (id == doesNotExist) { ResourceWindowsCategory rwc = new ResourceWindowsCategory(); rwc.Name = name; rwc.CategoryType = categoryType; entities.AddToResourceWindowsCategories(rwc); entities.SaveChanges(); ResourceWindowsCategory newRWC = SelectByName(entities, rwc.Name, categoryType); return(newRWC.CategoryId); } else { return(id); } }
/// <summary> /// Adds a new set of PerfMon counters. /// </summary> /// <param name="entities">The data context.</param> /// <param name="categoryName">The category name.</param> /// <param name="instanceName">The instance name.</param> /// <param name="counterName">The counter name.</param> public static void AddPerfMon(EnterpriseTestEntities entities, string categoryName, string instanceName, string counterName, ResourceWindowsCategoryType categoryType) { string categoryTypeName = categoryType.ToString(); ResourceWindowsCategory category = null; ResourceWindowsCategory instance = null; ResourceWindowsCategory counter = Select(entities, categoryTypeName, counterName).FirstOrDefault(); if (counter == null) { counter = ResourceWindowsCategory.CreateResourceWindowsCategory(NextId(entities), counterName, categoryTypeName); } //If instance is blank, use the constant string. This is because in the ResourceWindowsCategory table the blank category is the root, //so we can't reuse it further down in the hierarchy. instance = GetParent(entities, instanceName == string.Empty ? InstanceDoesNotApply : instanceName, counter, categoryTypeName); category = GetParent(entities, categoryName, instance, categoryTypeName); //Wire up the parent relationships so that the EF can handle it. if (counter.EntityState == System.Data.EntityState.Detached) { counter.Parents.Add(instance); } if (instance.EntityState == System.Data.EntityState.Detached) { instance.Parents.Add(category); } if (category.EntityState == System.Data.EntityState.Detached) { //We're adding a new category, so hook it up the the root parent ResourceWindowsCategory root = Select(entities, categoryTypeName, string.Empty).FirstOrDefault(); if (root != null) { category.Parents.Add(root); } } }