private void BuildPropertyValues(MobilePlantComponent componentIn, MobilePlantComponentDataAdapter adapter) { foreach (var pair in adapter.PropertyValues) { //DOES PROPERTY EXIST? MobilePlantProperty property = (from x in mExistingEquipmentProperties where x.Name.ToLower() == pair.Key.ToLower() select x).FirstOrDefault(); if (property == null) { if (CanCreateProperties) { property = new MobilePlantProperty { Name = pair.Key, DefaultValue = pair.Value, Description = " (created by importer)." }; mExistingEquipmentProperties.Add(property); //update cache } else { //ERROR! RaiseMessage(CommonUtils.MessageType.Error, string.Format("WorkSheet '{0}' Row '{1} Tag {2} Component Name {3}' : The property does not exist.", WorkSheetName, adapter.RowNumber, adapter.Tag, adapter.ComponentName)); continue; } } //CHECK MobilePlantComponentTypeProperty Exists MobilePlantComponentTypeProperty equipmentComponentTypeProperty = null; if (mExistingEquipmentComponentTypeProperty.Any()) { equipmentComponentTypeProperty = (from x in mExistingEquipmentComponentTypeProperty where x.MobilePlantComponentType.Name.ToLower() == componentIn.MobilePlantComponentType.Name.ToLower() && x.MobilePlantProperty.Name.ToLower() == property.Name.ToLower() select x).FirstOrDefault(); } if (equipmentComponentTypeProperty == null) { if (CanCreateProperties) { //CREATE JOIN ROW equipmentComponentTypeProperty = new MobilePlantComponentTypeProperty(); equipmentComponentTypeProperty.MobilePlantComponentType = componentIn.MobilePlantComponentType; //note: set the object! equipmentComponentTypeProperty.MobilePlantProperty = property; //not set the object! mExistingEquipmentComponentTypeProperty.Add(equipmentComponentTypeProperty); //update cache } else { //ERROR! RaiseMessage(CommonUtils.MessageType.Warning, string.Format("WorkSheet '{0}' Row '{1} Tag {2} Component Type {3}' : The property {4} does not belong to the Component Type.", WorkSheetName, adapter.RowNumber, adapter.Tag, componentIn.MobilePlantComponentType.Name, property.Name)); continue; } } property.MobilePlantComponentTypeProperties.Add(equipmentComponentTypeProperty); //CHECK PROPERTYVALUE EXISTS MobilePlantPropertyValue propertyValue = null; if (mExistingPropertyValues.Any()) { propertyValue = (from x in mExistingPropertyValues where x.MobilePlantComponent.Name.ToLower() == componentIn.Name.ToLower() && x.MobilePlantProperty.Name.ToLower() == property.Name.ToLower() select x).FirstOrDefault(); } if (propertyValue == null) { propertyValue = new MobilePlantPropertyValue(); propertyValue.MobilePlantComponent = componentIn; propertyValue.MobilePlantProperty = property; mExistingPropertyValues.Add(propertyValue); //update cache } //set value if (!string.IsNullOrEmpty(pair.Value)) { propertyValue.Value = pair.Value.ChangeNullToEmptyString(); } componentIn.MobilePlantPropertyValues.Add(propertyValue); } }
public MobilePlantProperty SaveMobilePlantProperty(MobilePlantProperty mobilePlantProperty) { using (CmsEntities cee = new CmsEntities()) { MobilePlantProperty original = (from x in cee.MobilePlantProperties where x.Id == mobilePlantProperty.Id select x).FirstOrDefault(); if (original == null) { cee.MobilePlantProperties.Add(mobilePlantProperty); } else { cee.Entry(original).CurrentValues.SetValues(mobilePlantProperty); } cee.SaveChanges(); } return mobilePlantProperty; }