public void Write(System.Data.IDataReader reader) { var organization = db.Organizations.Find(organizationId); Dictionary <string, KitMaterial> materials = new Dictionary <string, KitMaterial>(); Dictionary <string, KitOption> options = new Dictionary <string, KitOption>(); Dictionary <int, ValveInterfaceCode> valveInterfaces = new Dictionary <int, ValveInterfaceCode>(); Dictionary <int, ActuatorInterfaceCode> actuatorInterfaces = new Dictionary <int, ActuatorInterfaceCode>(); while (reader.Read()) { // columns // KitNumber, ValveIntCode, ActIntCode, MaterialCode, OptionCode, Price string kitNumber = reader.GetString(0); var kit = db.Kits.Where(k => k.KitNumber == kitNumber).FirstOrDefault(); if (kit == null) { // get the material string materialCode = reader.GetString(3); var material = db.KitMaterials.Where(m => m.Code == materialCode).FirstOrDefault(); if (material == null) { if (materials.ContainsKey(materialCode)) { material = materials[materialCode]; } else { material = new KitMaterial() { Code = materialCode, Name = materialCode }; db.KitMaterials.Add(material); materials.Add(materialCode, material); } } // get the locking option string optionCode = reader.GetString(4); var option = db.KitOptions.Where(o => o.Code == optionCode).FirstOrDefault(); if (option == null) { if (options.ContainsKey(optionCode)) { option = options[optionCode]; } else { option = new KitOption() { Code = optionCode, Name = optionCode }; db.KitOptions.Add(option); options.Add(optionCode, option); } } // get the valve interface int valveIntCode = reader.GetInt32(1); var valveInterface = db.ValveInterfaceCodes.Find(valveIntCode); if (valveInterface == null) { if (valveInterfaces.ContainsKey(valveIntCode)) { valveInterface = valveInterfaces[valveIntCode]; } else { valveInterface = new ValveInterfaceCode() { InterfaceCode = valveIntCode }; db.ValveInterfaceCodes.Add(valveInterface); valveInterfaces.Add(valveIntCode, valveInterface); } } // get the actuator interace int actIntCode = reader.GetInt32(2); var actInterface = db.ActuatorInterfaceCodes.Find(actIntCode); if (actInterface == null) { if (actuatorInterfaces.ContainsKey(actIntCode)) { actInterface = actuatorInterfaces[actIntCode]; } else { actInterface = new ActuatorInterfaceCode() { InterfaceCode = actIntCode }; db.ActuatorInterfaceCodes.Add(actInterface); actuatorInterfaces.Add(actIntCode, actInterface); } } // add the kit double price = reader.GetDouble(5); kit = new Kit() { KitNumber = reader.GetString(0), ValuveIntefaceCodeEntity = valveInterface, ActuatorInterfaceCodeEntity = actInterface, Price = price, Material = material, Option = option }; db.Kits.Add(kit); } // link this kit to the organization if (kit.KitId > 0) { if (!organization.Kits.Where(k => k.KitId == kit.KitId).Any()) { organization.Kits.Add(kit); } } else { organization.Kits.Add(kit); } } }
public void Write(IDataReader reader) { var organization = db.Organizations.Find(organizationId); HashSet <string> map = new HashSet <string>(); while (reader.Read()) { // columns // mfg, model, size, ValveIntCode // get the valve interface int valveIntCode = reader.GetInt32(3); string mfg = reader.GetString(0); string model = reader.GetString(1); string size = reader.GetString(2); var valveInterface = db.ValveInterfaceCodes.Find(valveIntCode); if (valveInterface == null) { valveInterface = new ValveInterfaceCode() { InterfaceCode = valveIntCode }; db.ValveInterfaceCodes.Add(valveInterface); } var valve = db.Valves .Where(v => v.Manufacturer == mfg) .Where(v => v.Model == model) .Where(v => v.Size == size).FirstOrDefault(); if (valve == null) { if (!map.Contains($"{mfg} {model} {size}")) { map.Add($"{mfg} {model} {size}"); valve = new Valve() { Manufacturer = reader.GetString(0), Model = reader.GetString(1), Size = reader.GetString(2), InterfaceCode = valveIntCode }; db.Valves.Add(valve); organization.Valves.Add(valve); } } // link this valve to the organization if (valve != null) { if (valve.ValveId > 0) { if (!organization.Valves.Where(v => v.ValveId == valve.ValveId).Any()) { organization.Valves.Add(valve); } } } } }