Example #1
0
        public static SupplierAPIData AddSupplier(SupplierAPIData nSupplier, bool doCommit = true)
        {
            if (nSupplier.CompanyName.Length > 40)
            {
                throw new FormatException("CompanyName must be less than or equal to 40 characters.");
            }
            using (var db = new NorthwindDbContext())
            {
                Supplier supplier = db.Suppliers.Add(new Supplier
                {
                    CompanyName = nSupplier.CompanyName
                });

                if (doCommit)
                {
                    db.SaveChanges();
                }

                return new SupplierAPIData(supplier);
            }
        }
Example #2
0
        public static SupplierAPIData UpdateSupplier(int supplierID, SupplierAPIData nSupplier, bool doCommit = true)
        {
            if (nSupplier.CompanyName.Length > 40)
            {
                throw new FormatException("CompanyName must be less than or equal to 40 characters.");
            }

            using (var db = new NorthwindDbContext())
            {
                Supplier supplier = db.Suppliers.Find(supplierID);

                // only update the company name
                supplier.CompanyName = nSupplier.CompanyName;

                if (doCommit)
                {
                    db.SaveChanges();
                }

                return new SupplierAPIData(supplier);
            }
        }