public ActionResult Create(Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.ReportsTo = new SelectList(db.Employees, "EmployeeID", "LastName", employee.ReportsTo);
            return View(employee);
        }
 public ActionResult Edit(Employee employee)
 {
     if (ModelState.IsValid)
     {
         db.Entry(employee).State = EntityState.Modified;
         db.SaveChanges();
         return RedirectToAction("Index");
     }
     ViewBag.ReportsTo = new SelectList(db.Employees, "EmployeeID", "LastName", employee.ReportsTo);
     return View(employee);
 }
        public async Task EmployeeMustBeFromUs()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var employee = new Employee { FirstName = "Bill", LastName = "Gates" };
            manager.AttachEntity(employee);

            try
            {
                // add the US-only validator
                employee.EntityAspect.EntityType.GetProperty("Country")
                        .Validators.Add(new CountryIsUsValidator());

                // non-US customers no longer allowed 
                employee.Country = "Canada";
                var validationErrors = employee.EntityAspect.ValidationErrors;
                Assert.IsTrue(validationErrors.Any(ve => ve.Message.Contains("Country must start with 'US'")), "Should be a validation error stating Country must start with 'US'.");

                // back in the USA 
                employee.Country = "USA";
                Assert.IsFalse(validationErrors.Any(), "Should be no validation errors.");
            }
            finally
            {
                Assert.IsTrue(employee.EntityAspect.EntityType.GetProperty("Country")
                        .Validators.Remove(new CountryIsUsValidator()));                
            }
        }
        public async Task EmployeeMustHaveValidUSPhone()
        {
            var manager = new EntityManager(_serviceName); // new empty EntityManager
            await manager.FetchMetadata(); // required before creating a new entity

            var employeeType = manager.MetadataStore.GetEntityType(typeof(Employee)); //get the Employee type

            var phoneValidator = new PhoneNumberValidator();
            // or the hard way ... :)
            // var phoneValidator = new RegexValidator(@"^((\([2-9]\d{2}\) ?)|([2-9]\d{2}[-.]))\d{3}[-.]\d{4}$", "phone number"); // email pattern
            try
            {
                // add regex validator that validates emails to the HomePhone property
                employeeType.GetProperty("HomePhone").Validators.Add(phoneValidator); 

                var employee = new Employee { FirstName = "Jane", LastName = "Doe" };
                employee.HomePhone = "N2L 3G1"; // a bad phone number

                // force validation of unattached employee
                manager.AttachEntity(employee);
                var errors = employee.EntityAspect.ValidationErrors;

                Assert.IsTrue(errors.Any(), String.Format("should have 1 error: {0}", errors.First().Message));

                employee.HomePhone = "510-555-1111";

                Assert.IsFalse(errors.Any(), String.Format("should have no errors"));
            }
            finally
            {
                Assert.IsTrue(employeeType.GetProperty("HomePhone").Validators.Remove(phoneValidator));
            }
        }
Пример #5
0
        public async Task RemovingNavigationProperty() {
          var entityManager = await TestFns.NewEm(_northwindServiceName);

          var employee = new Employee() {
            FirstName = "First",
            LastName = "Employee"
          };
          entityManager.AddEntity(employee);


          var manager = new Employee() {
            FirstName = "First",
            LastName = "Manager"
          };
          entityManager.AddEntity(manager);
          employee.Manager = manager;

          try {
            var saveResult = await entityManager.SaveChanges();

            // Now reverse everything
            manager.EntityAspect.Delete();
            employee.Manager = null;

            employee.EntityAspect.Delete();

            saveResult = await entityManager.SaveChanges();

          } catch (Exception e) {
            var message = string.Format("Save should have succeeded;  Received {0}: {1}",
                                        e.GetType().Name, e.Message);
            Assert.Fail(message);
          }

        }