public IEnumerable<string> ReadPartNames() { var names = new List<string>(); using (var db = new CarPartsDbContext()) { names = db.Parts .Select(p => p.Name) .ToList(); } return names; }
private void MigrateManufacturers(MongoDbHandler mongoHandler) { var manufacturers = mongoHandler.ReadCollection("Manufacturers").Result; using (var db = new CarPartsDbContext()) { foreach (var manufacturer in manufacturers) { var sqlManufacturer = new Manufacturer(); sqlManufacturer.Id = manufacturer["_id"].AsInt32; sqlManufacturer.Name = manufacturer["Name"].AsString; db.Manufacturers.Add(sqlManufacturer); } db.SaveChanges(); } }
private void MigrateParts(MongoDbHandler mongoHandler) { var parts = mongoHandler.ReadCollection("Parts").Result; using (var db = new CarPartsDbContext()) { foreach (var part in parts) { var sqlPart = new Part(); sqlPart.Id = part["_id"].AsInt32; sqlPart.Name = part["Name"].AsString; sqlPart.Price = decimal.Parse(part["Price"].AsString); sqlPart.ManufacturerId = part["ManufacturerId"].AsInt32; sqlPart.Manufacturer = db.Manufacturers.Where(m => m.Id == sqlPart.ManufacturerId) .FirstOrDefault(); foreach (var categoryId in part["Categories"] as BsonArray) { int category = categoryId.AsInt32; sqlPart.Categories.Add((PartCategory)category); } var vendorIds = new List<int>(); foreach (var vendorId in part["VendorIds"] as BsonArray) { vendorIds.Add(vendorId.AsInt32); } sqlPart.Vendors = db.Vendors.Where(v => vendorIds.Contains(v.Id)).ToList(); sqlPart.Manufacturer.Parts.Add(sqlPart); foreach (var vendor in sqlPart.Vendors) { vendor.Parts.Add(sqlPart); } db.Parts.Add(sqlPart); } db.SaveChanges(); } }
public IEnumerable<PartReportInputModel> ReadPartReports() { var reports = new List<PartReportInputModel>(); using (var db = new CarPartsDbContext()) { reports = db.Sales.GroupBy(s => s.PartId) .Select(gr => gr.FirstOrDefault()) .Select(s => new PartReportInputModel { PartId = s.PartId, PartName = s.Part.Name, Price = s.UnitPrice, Vendor = s.Vendor.Name, Quantity = s.Quantity, TotalPrice = s.Quantity * s.UnitPrice }) .ToList(); } return reports; }
public void LoadCountries(IEnumerable<XmlCountry> collection) { var countries = new List<Country>(); using (var db = new CarPartsDbContext()) { foreach (var xmlCountry in collection) { var country = new Country(); country.Id = xmlCountry.Id; country.Name = xmlCountry.Name; var vendors = db.Vendors.Where(v => xmlCountry.VendorsIds.Contains(v.Id)) .ToList(); foreach (var vendor in vendors) { country.Vendors.Add(vendor); vendor.Countries.Add(country); } var manufacturers = db.Manufacturers.Where(m => xmlCountry.ManufacturersIds.Contains(m.Id)) .ToList(); foreach (var manufacturer in manufacturers) { country.Manufacturers.Add(manufacturer); manufacturer.Countries.Add(country); } db.Countries.Add(country); } db.SaveChanges(); } }
private void MigrateVendors(MongoDbHandler mongoHandler) { var vendors = mongoHandler.ReadCollection("Vendors").Result; using (var db = new CarPartsDbContext()) { foreach (var vendor in vendors) { var sqlVendor = new Vendor(); sqlVendor.Id = vendor["_id"].AsInt32; sqlVendor.Name = vendor["Name"].AsString; db.Vendors.Add(sqlVendor); } db.SaveChanges(); } }