private static void SaveProducs(List<Product> productsTobeImported) { try { AaauctionEntities db = new AaauctionEntities(); foreach (var product in productsTobeImported) { var stock_no = db.vehicle_for_sale.Where(x => x.stock_no == product.Stock_No).Select(y => y.stock_no).FirstOrDefault(); if(stock_no > 0) { continue; } var makerId = db.makes.Where(x => x.make_name == product.Maker).Select(y => y.makeid).FirstOrDefault(); //break; if (makerId == 0) { var make = db.makes.Add(new make() { make_name = product.Maker }); db.SaveChanges(); makerId = make.makeid; } var modelId = db.models.Where(x => x.make_id == makerId && x.model_name == product.Model).Select(y => y.id).FirstOrDefault(); if(modelId == 0 ) { var model = db.models.Add(new model() { model_name = product.Model, make_id = makerId }); db.SaveChanges(); modelId = model.id; } var categoryId = db.vehicle_category.Where(x => x.category == product.ProductType).Select(y => y.categoryid).FirstOrDefault(); if(categoryId == 0) { var category = db.vehicle_category.Add(new vehicle_category() { category = product.ProductType }); db.SaveChanges(); categoryId = category.categoryid; } if(stock_no == 0) { var addProduct = new vehicle_for_sale(); addProduct.is_sold = false; addProduct.model_id = modelId; addProduct.vehicle_category_id = categoryId; addProduct.CC = product.CC; addProduct.chassis_no_1 = product.chassis_no_1; addProduct.chassis_no_2 = product.chassis_no_2; addProduct.color = product.Color; addProduct.ETD = product.ETD; addProduct.gear_at = product.Gear_m_at == "A/T" ? true : false; addProduct.grade = product.Grade; addProduct.KM_ran = product.KM_ran; addProduct.last_modified_date = DateTime.Now; addProduct.month = product.Month; addProduct.year = product.Year; addProduct.no_of_doors = product.NoOfDoors; addProduct.price = product.Price; addProduct.fuel = product.Fuel; addProduct.stock_no = product.Stock_No; var vehicleForSale = db.vehicle_for_sale.Add(addProduct); db.SaveChanges(); stock_no = vehicleForSale.stock_no; } if(product.Equipments.AC == true) { AddFeature(db, "AC",stock_no); } if(product.Equipments.FourWheelDrive == true) { AddFeature(db, "FourWheelDrive",stock_no); } if (product.Equipments.PS == true) { AddFeature(db, "PS", stock_no); } if (product.Equipments.PW == true) { AddFeature(db, "PW", stock_no); } if (product.Equipments.WCAB == true) { AddFeature(db, "WCAB", stock_no); } foreach(var image in product.Images) { var imageId = db.images_on_vehicles_for_sale.Where(x => x.stock_no == stock_no && x.imageurl == image).Select(y => y.imageid).FirstOrDefault(); if (imageId == 0) { db.images_on_vehicles_for_sale.Add(new images_on_vehicles_for_sale() { imageurl = image, stock_no = stock_no }); db.SaveChanges(); } } } db.SaveChanges(); } catch (Exception ex) { throw; } }
private static void SaveProducs(List <Product> productsTobeImported) { try { AaauctionEntities db = new AaauctionEntities(); foreach (var product in productsTobeImported) { var stock_no = db.vehicle_for_sale.Where(x => x.stock_no == product.Stock_No).Select(y => y.stock_no).FirstOrDefault(); if (stock_no > 0) { continue; } var makerId = db.makes.Where(x => x.make_name == product.Maker).Select(y => y.makeid).FirstOrDefault(); //break; if (makerId == 0) { var make = db.makes.Add(new make() { make_name = product.Maker }); db.SaveChanges(); makerId = make.makeid; } var modelId = db.models.Where(x => x.make_id == makerId && x.model_name == product.Model).Select(y => y.id).FirstOrDefault(); if (modelId == 0) { var model = db.models.Add(new model() { model_name = product.Model, make_id = makerId }); db.SaveChanges(); modelId = model.id; } var categoryId = db.vehicle_category.Where(x => x.category == product.ProductType).Select(y => y.categoryid).FirstOrDefault(); if (categoryId == 0) { var category = db.vehicle_category.Add(new vehicle_category() { category = product.ProductType }); db.SaveChanges(); categoryId = category.categoryid; } if (stock_no == 0) { var addProduct = new vehicle_for_sale(); addProduct.is_sold = false; addProduct.model_id = modelId; addProduct.vehicle_category_id = categoryId; addProduct.CC = product.CC; addProduct.chassis_no_1 = product.chassis_no_1; addProduct.chassis_no_2 = product.chassis_no_2; addProduct.color = product.Color; addProduct.ETD = product.ETD; addProduct.gear_at = product.Gear_m_at == "A/T" ? true : false; addProduct.grade = product.Grade; addProduct.KM_ran = product.KM_ran; addProduct.last_modified_date = DateTime.Now; addProduct.month = product.Month; addProduct.year = product.Year; addProduct.no_of_doors = product.NoOfDoors; addProduct.price = product.Price; addProduct.fuel = product.Fuel; addProduct.stock_no = product.Stock_No; var vehicleForSale = db.vehicle_for_sale.Add(addProduct); db.SaveChanges(); stock_no = vehicleForSale.stock_no; } if (product.Equipments.AC == true) { AddFeature(db, "AC", stock_no); } if (product.Equipments.FourWheelDrive == true) { AddFeature(db, "FourWheelDrive", stock_no); } if (product.Equipments.PS == true) { AddFeature(db, "PS", stock_no); } if (product.Equipments.PW == true) { AddFeature(db, "PW", stock_no); } if (product.Equipments.WCAB == true) { AddFeature(db, "WCAB", stock_no); } foreach (var image in product.Images) { var imageId = db.images_on_vehicles_for_sale.Where(x => x.stock_no == stock_no && x.imageurl == image).Select(y => y.imageid).FirstOrDefault(); if (imageId == 0) { db.images_on_vehicles_for_sale.Add(new images_on_vehicles_for_sale() { imageurl = image, stock_no = stock_no }); db.SaveChanges(); } } } db.SaveChanges(); } catch (Exception ex) { throw; } }