public Sale(Vendor vendor, int productId, Supermarket supermarket, decimal unitPrice, int quantity) { this.vendor = vendor; this.ProductId = productId; this.supermarket = supermarket; this.UnitPrice = unitPrice; this.Quantity = quantity; this.Sum = this.Quantity * this.UnitPrice; this.Date = DateTime.Now; }
public static void ImportExpensesInDatabase(string fileName) { var context = new MsSqlEntities(); XDocument doc = XDocument.Load(fileName); var vendorsXml = doc.XPathSelectElements("expenses-by-month/vendor"); foreach (var vendorXml in vendorsXml) { string vendorName = vendorXml.Attribute("name").Value; var expensesXml = vendorXml.XPathSelectElements("expenses"); foreach (var expsenseXml in expensesXml) { DateTime month = DateTime.Parse(expsenseXml.Attribute("month").Value); decimal sum = decimal.Parse(expsenseXml.Value); Vendor vendor = context.Vendors .FirstOrDefault(v => v.Name == vendorName); if (vendor == null) { Vendor newVendor = new Vendor(); newVendor.Name = vendorName; context.Vendors.Add(newVendor); context.SaveChanges(); vendor = newVendor; } int vendorId = vendor.Id; Expense newExpense = new Expense(); newExpense.Date = month; newExpense.Sum = sum; newExpense.VendorId = vendorId; context.Expenses.Add(newExpense); } } context.SaveChanges(); }