public static void SetData( List<VendorInformation> vendors, List<MeasureInformation> measures, List<ProductInformation> products) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<SqlServerEntities, Configuration>()); using (var db = new SqlServerEntities()) { foreach (var vend in vendors) { db.Vendors.Add(new Vendor { VendorId = vend.VendorId, VendorName = vend.VendorName }); } foreach (var meas in measures) { db.Measures.Add(new Measure { MeasureId = meas.MeasureId, MeasureName = meas.MeasureName }); } foreach (var prod in products) { db.Products.Add(new Product { ProductName = prod.ProductName, BasePrice = (decimal)prod.BasePrice, MeasureId = prod.MeasureId, VendorId = prod.VendorId }); } db.SaveChanges(); } }
public static void SetDataFromXls(List<List<SaleInfo>> reports) { Database.SetInitializer(new MigrateDatabaseToLatestVersion<SqlServerEntities, Configuration>()); using (var db = new SqlServerEntities()) { foreach (var file in reports) { foreach (var rep in file) { var supermarket = db.Supermarkets.FirstOrDefault(x => x.SupermarketName == rep.Location); if (supermarket == null) { supermarket = new Supermarket { SupermarketName = rep.Location }; db.Supermarkets.Add(supermarket); } var sale = new Sale { ProductId = rep.ProductId, Supermarket = supermarket, Date = rep.SaleDate, Quantity = rep.Quantity, UnitPrice = rep.UnitPrice, Sum = rep.Sum }; db.Sales.Add(sale); db.SaveChanges(); } } } }