Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i tried some code like this

in vs 2013 i wrote code in "Web Api Config"

C#
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;
//using ODataService.Models;





namespace ODataService
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();
            config.MapODataServiceRoute("odataRoute", "odata", GetModel());

            //config.Services.Replace(typeof(IHttpActionSelector), new System.Web.Http.OData.Routing.ODataActionSelector());
           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");
            ////builder.EntitySet<tbl_Manager>("Mangers");
            //return builder.GetEdmModel();

            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


C#
public static IEdmModel GetModel()
       {
           var builder = new ODataConventionModelBuilder();
           builder.EntitySet<ODataServiceEntities>("tbl_Employee");
         //  builder.EntitySet<ODataServiceEntities>("tbl_Employees");

           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.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900