i tried some code like this
in vs 2013 i wrote code in "Web Api Config"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.OData.Builder;
using Microsoft.OData.Edm;
using ODataService.Models;
using System.Web.OData.Extensions;
using System.Web.OData;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
namespace ODataService
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.MapODataServiceRoute("odataRoute", "odata", GetModel());
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public static IEdmModel GetModel()
{
DurgaEntities1 obj=new DurgaEntities1();
var builder = new ODataConventionModelBuilder();
builder.EntitySet<tbl_Employee>("tbl_Employees");
var ee = from vv in obj.tbl_Employee.AsEnumerable() select vv;
return builder.GetEdmModel();
}
}
}
i took one controller and write some code like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Routing;
using Odatawithwebapi.Models;
namespace Odatawithwebapi.Controllers
{
[ODataRoutePrefix("tbl_Employee")]
public class EmployeeController : ODataController
{
ODataServiceEntities _Entity = new ODataServiceEntities();
[ODataRoute]
[EnableQuery]
public IHttpActionResult GetEmployeesList()
{
return Ok(_Entity.tbl_Employee);
}
[ODataRoute("({C_Employee_Eno})")]
[EnableQuery]
public IHttpActionResult GetEmployeeusingeno(int _Employeeno)
{
return Ok(SingleResult.Create<tbl_employee>(_Entity.tbl_Employee.Where(eno => eno.C_Employee_Eno == _Employeeno)));
}
[ODataRoute]
public IHttpActionResult GetListEmployee(tbl_Employee _EmpList)
{
_Entity.tbl_Employee.Add(_EmpList);
_Entity.SaveChanges();
return Ok(_EmpList);
}
[ODataRoute("({C_Employee_Eno})")]
public IHttpActionResult DeleteEmployee(int _Employeeno)
{
var _EmployeeDelete = _Entity.tbl_Employee.Where(eno => eno.C_Employee_Eno == _Employeeno).FirstOrDefault();
if (null != _EmployeeDelete)
{
_Entity.tbl_Employee.Remove(_EmployeeDelete);
}
return Ok();
}
}
}
but run/debug my application i got error in "WebApiConfig" and like this
public static IEdmModel GetModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<ODataServiceEntities>("tbl_Employee");
return builder.GetEdmModel();
}
An exception of type 'System.InvalidOperationException' occurred in System.Web.OData.dll but was not handled in user code
Additional information: The entity 'ODataServiceEntities' does not have a key defined.