0

我使用WebApi. 这是名为 的控制器类 Employee Controller

namespace HelloWebApi.Controllers
{
    public class EmployeesController : ApiController
    {
        private static IList<Employee> list = new List<Employee>()
        {
            new Employee()
            {
            Id = 12345, FirstName = "John", LastName = "Human",Department = 2
            },
            new Employee()
            {
            Id = 12346, FirstName = "Jane", LastName = "Public",Department = 3
            },
            new Employee()
            {
            Id = 12347, FirstName = "Joseph", LastName = "Law",Department = 2
            }
        };
 // GET api/employees
        public IEnumerable<Employee> Get()
        {
            return list;
        }

        // GET api/employees/12345
        public Employee Get(int id)
        {
            return list.First(e => e.Id == id);
        }
        // POST api/employees
        public void Post(Employee employee)
        {
            int maxId = list.Max(e => e.Id);
            employee.Id = maxId + 1;
            list.Add(employee);
        }
        // PUT api/employees/12345
        public void Put(int id, Employee employee)
        {
            int index = list.ToList().FindIndex(e => e.Id == id);
            list[index] = employee;
        }
        // DELETE api/employees/12345
        public void Delete(int id)
        {
            Employee employee = Get(id);
            list.Remove(employee);
        }

        public HttpResponseMessage Patch(int id, Delta<Employee> deltaEmployee)
        {
            var employee = list.FirstOrDefault(e => e.Id == id);
            if (employee == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            deltaEmployee.Patch(employee);
            return Request.CreateResponse(HttpStatusCode.NoContent);
        }
    }
}

在这里,我使用所有 HTTP 动词方法进行插入、更新、删除。我使用了一个存储 Employee 对象的列表。插入、删除和更新都基于这个列表。但是这个程序没有数据库连接。我想知道如何在这个程序中使用数据库访问。我想使用数据库中的表。我该怎么做?请任何有用的链接。

4

0 回答 0