public class EmployeeController : ODataController
{
private HRMContext db = new HRMContext();
// GET: odata/Employee
[EnableQuery]
public IQueryable<Employee> GetEmployee()
{
return db.Employees;
}
// GET: odata/Employee(5)
[EnableQuery]
public SingleResult<Employee> GetEmployee([FromODataUri] int key)
{
return SingleResult.Create(db.Employees.Where(employee => employee.Id == key));
}
// PUT: odata/Employee(5)
public IHttpActionResult Put([FromODataUri] int key, Delta<employee> patch)
{
Validate(patch.GetEntity());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Employee employee = db.Employees.Find(key);
if (employee == null)
{
return NotFound();
}
patch.Put(employee);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(employee);
}
// POST: odata/Employee
public IHttpActionResult Post(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
db.SaveChanges();
return Created(employee);
}
// PATCH: odata/Employee(5)
[AcceptVerbs("PATCH", "MERGE")]
public IHttpActionResult Patch([FromODataUri] int key, Delta<employee> patch)
{
Validate(patch.GetEntity());
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
Employee employee = db.Employees.Find(key);
if (employee == null)
{
return NotFound();
}
patch.Patch(employee);
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(employee);
}
// DELETE: odata/Employee(5)
public IHttpActionResult Delete([FromODataUri] int key)
{
Employee employee = db.Employees.Find(key);
if (employee == null)
{
return NotFound();
}
db.Employees.Remove(employee);
db.SaveChanges();
return StatusCode(HttpStatusCode.NoContent);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(int key)
{
return db.Employees.Count(e => e.Id == key) > 0;
}
}
</employee></employee>
,