public static void AddCustomer(Customers customer) { try { _log.Info(l => l("Start adding a new customer with PK: {0}", customer.CustomerID)); using (var dbContext = new NorthwindDbContext()) { var validation = dbContext.GetValidationErrors(); dbContext.Customers.Add(customer); dbContext.SaveChanges(); } } catch (DbEntityValidationException dbEx) { foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { _log.Error(l => l("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage)); } } } catch (ExecutionEngineException ex) { throw ex; } }
private void btnTestValidation_Click(object sender, EventArgs e) { var newCustomer = new Customers() { CustomerID = "XXHA", CompanyName = "Queaso Systems NV", ContactName = "Ruben Haeck", ContactTitle = "ICT Architect/Senior Developer", Address = "Hendrik Waelputstraat 21", City = "Ghent", PostalCode = "9000", Country = "Belgium", Phone = "092260000" }; CustomerManager.AddCustomer(newCustomer); }
private void btnAdd_Click(object sender, EventArgs e) { var random = new Random(); var newCustomer = new Customers() { CustomerID = string.Format("RHK{0}", random.Next(0, 12)), CompanyName = "Queaso Systems NV", ContactName = "Ruben Haeck", ContactTitle = "ICT Architect/Senior Developer", Address = "Hendrik Waelputstraat 21", City = "Ghent", PostalCode = "9000", Country = "Belgium", Phone = "092260000" }; CustomerManager.AddCustomer(newCustomer); }