Monday, 15 September 2014

Microsoft Web API 2 Application using EntityFramework 6.0 supporting oData v4




Web API 2 Application using EntityFramework 6.0 supporting oData v4

  • Using Microsoft Visual Studio Express 2013 for Web
  • Create new Project Alerts  of Template  WebAPI
  • The project is targeted for .NET Framework 4.5
  • Using NuGet Package Manager update/add all the latest packages as listed in the Packages.config shown below


<?xml version="1.0" encoding="utf-8"?>
    <packages>
      <package id="EntityFramework" version="6.0.0" targetFramework="net45" />
      <package id="Microsoft.AspNet.WebApi" version="5.2.2" targetFramework="net45" />
      <package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
      <package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />
      <package id="Microsoft.AspNet.WebApi.OData" version="5.3.0" targetFramework="net45" />
      <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.2" targetFramework="net45" />
      <package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
      <package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
      <package id="Microsoft.OData.Client" version="6.7.0" targetFramework="net45" />
      <package id="Microsoft.OData.Core" version="6.7.0" targetFramework="net45" />
      <package id="Microsoft.OData.Edm" version="6.7.0" targetFramework="net45" />
      <package id="Microsoft.Spatial" version="6.7.0" targetFramework="net45" />
      <package id="Newtonsoft.Json" version="6.0.5" targetFramework="net45" />
      <package id="System.Spatial" version="5.6.2" targetFramework="net45" />
    </packages>


    Create entity data model for the database


    • Add->New Item->Data->ADO.NET Entity Data Model
    • Name it as AlertsModel.edmx
    • Chose Model Contents -> Generate from database
    • In Next step , create connection string by selecting your database and select entity connection settings in Web.config with name as AlertsEntities
    • In Next step, select the tables and views you want to expose, in our example view called AlertsView1 and name Model namespace as AlertsModel

    Add Controller derived from oDataController

    This either we could create manually or use oData scaffolding

    Manual Creation:
    • Controllers->Add->Controller->Web API 2 Controller - Empty
    • Name the Controller as AlertsController
    • Modify the class to derive from ODataController
    • Add Get method as follows


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web.OData;

    namespace Alerts.Controllers
    {
        public class AlertsController : ODataController
        {
            AlertsEntities db = new AlertsEntities();
          
            protected override void Dispose(bool disposing)
            {
                db.Dispose();
                base.Dispose(disposing);
            }
            [EnableQuery]
            public IQueryable<AlertsView1> Get()
            {
             
                return db.AlertsView1;
            }
        }
    }

    Adding  a route for this controller

    • Edit WebApiConfig.cs file under App_Start folder
    • Remove the HttpRoute entries and add Odata route as follows

    using Microsoft.OData.Edm;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.Http;
    using System.Web.OData.Builder;
    using System.Web.OData.Extensions;

    namespace Alerts
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {

                config.MapODataServiceRoute(
                    routeName: "ODataRoute",
                    routePrefix: null,
                    model: GenerateEdmModel());
            }
           

            private static IEdmModel GenerateEdmModel()
            {
                 var builder = new ODataConventionModelBuilder();
                 builder.EntitySet<Alerts.AlertsView1>("Alerts");
                 return builder.GetEdmModel();
            }
        }
    }

    The EntitySet is named as Alerts and EntityType is named as AlertsView1

    In Web.config under runtime section add BindingRedirect for the newer versions of dll as follows


     <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">

          <dependentAssembly>    
            <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
          </dependentAssembly>

        <dependentAssembly>
            <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="5.0.0.0" newVersion="5.2.2.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="5.0.0.0" newVersion="5.2.2.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="Microsoft.OData.Core" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.7.0.0" newVersion="6.7.0.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="Microsoft.OData.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.7.0.0" newVersion="6.7.0.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-6.7.0.0" newVersion="6.7.0.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="5.6.0.0" newVersion="5.6.2.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="5.6.0.0" newVersion="5.6.2.0" />
          </dependentAssembly>

          <dependentAssembly>
            <assemblyIdentity name="Microsoft.Data.oData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="5.6.0.0" newVersion="5.6.2.0" />
          </dependentAssembly>
          
        </assemblyBinding>
      </runtime>


    Build and run the application under IIS Express 
    URL : http://localhost:58403/

    output:
    {
      "@odata.context":"http://localhost:58403/$metadata","value":[
        {
          "name":"Alerts","kind":"EntitySet","url":"Alerts"
        }
      ]
    }

    URL: http://localhost:58403/$metadata


    <edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">

    <edmx:DataServices>

    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Alerts">

    <EntityType Name="AlertsView1">

    <Key>

    <PropertyRef Name="id"/>
    </Key>

    <Property Name="id" Type="Edm.Int32" Nullable="false"/>

    <Property Name="AlertSubType" Type="Edm.String"/>

    <Property Name="AlertType" Type="Edm.String"/>

    <Property Name="LocationName" Type="Edm.String"/>
    <Property Name="alertMessage" Type="Edm.String"/>
    <Property Name="Payload" Type="Edm.String"/>

    </EntityType>
    </Schema>

    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Default">

    <EntityContainer Name="Container">

    <EntitySet Name="Alerts" EntityType="Alerts.AlertsView1"/>
    </EntityContainer>
    </Schema>
    </edmx:DataServices>
    </edmx:Edmx>


    Next You may Deploy it under IIS

    No comments:

    Post a Comment