public InstrumentProperty SaveInstrumentProperty(InstrumentProperty instrumentProperty) { using (CmsEntities cee = new CmsEntities()) { InstrumentProperty original = (from x in cee.InstrumentProperties where x.Id == instrumentProperty.Id select x).FirstOrDefault(); if (original == null) { cee.InstrumentProperties.Add(instrumentProperty); } else { cee.Entry(original).CurrentValues.SetValues(instrumentProperty); } cee.SaveChanges(); } return instrumentProperty; }
private void BuildPropertyValues(InstrumentComponent componentIn, InstrumentComponentDataAdapter adapter) { foreach (KeyValuePair<string, string> pair in adapter.PropertyValues) { //DOES PROPERTY EXIST? InstrumentProperty property = (from x in mExistingProperties where x.Name.ToLower() == pair.Key.ToLower() select x).FirstOrDefault(); if (property == null) { if (CanCreateProperties) { property = new InstrumentProperty { Name = pair.Key, DefaultValue = pair.Value, Description = " (created by importer)." }; mExistingProperties.Add(property);//update cache } else { //ERROR! RaiseMessage(CommonUtils.MessageType.Error, string.Format("WorkSheet '{0}' Line '{1} Tag {2} Component Name {3}' : The property does not exist.", WorkSheetName, adapter.RowNumber, adapter.Tag, adapter.ComponentName)); continue; } } //CHECK InstrumentComponentTypeProperty Exists InstrumentComponentTypeProperty equipmentComponentTypeProperty = null; if (mExistingEquipmentComponentTypeProperty.Any()) { equipmentComponentTypeProperty = (from x in mExistingEquipmentComponentTypeProperty where x.InstrumentComponentType.Name.ToLower() == componentIn.InstrumentComponentType.Name.ToLower() && x.InstrumentProperty.Name.ToLower() == property.Name.ToLower() select x).FirstOrDefault(); } if (equipmentComponentTypeProperty == null) { if (CanCreateProperties) { //CREATE JOIN ROW equipmentComponentTypeProperty = new InstrumentComponentTypeProperty { InstrumentComponentType = componentIn.InstrumentComponentType, InstrumentProperty = property }; 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.InstrumentComponentType.Name, property.Name)); continue; } } property.InstrumentComponentTypeProperties.Add(equipmentComponentTypeProperty); //CHECK PROPERTYVALUE EXISTS InstrumentPropertyValue propertyValue = null; //if (mCee.InstrumentPropertyValues.Any()) //{ propertyValue = (from x in Cee.InstrumentPropertyValues where x.InstrumentComponent.Name.ToLower() == componentIn.Name.ToLower() && x.InstrumentProperty.Name.ToLower() == property.Name.ToLower() select x).FirstOrDefault(); //} if (propertyValue == null) { propertyValue = new InstrumentPropertyValue { InstrumentComponent = componentIn, InstrumentProperty = property }; //mExistingPropertyValues.Add(propertyValue);//update cache } //set value if (!string.IsNullOrEmpty(pair.Value)) { propertyValue.Value = pair.Value.ChangeNullToEmptyString(); } componentIn.InstrumentPropertyValues.Add(propertyValue); } }