0
Odata Rest API SOA

Create the Odata Project using Visual Studio

We are going to design the Odata Web API Project, by following below steps we will create  project. For security and message interceptor  we will discuss in next blog.
Start Visual Studio Editor, Click File menu, select New > Project.
Expand Installed > Templates > Visual C# > Web, and select the ASP.NET Web Application template

In the New Project dialog, select the Empty template. Under “Add folders and core references…”, click Web API. Click OK.


Command Options
Via Solution
  Right click on Solution =>click on “Manage NuGet Packages”
Via PMC
PM>Install-Package Microsoft.AspNet.Odata



Now Install Entity Framework
PM>Install-Package EntityFramework


Generate the connection string
Just copy paste in Config if you face any issue
<connectionStrings>
    <add name=DemoContext connectionString=Data Source=(localdb)v11.0; Initial Catalog=DemoContext; Integrated Security=True; MultipleActiveResultSets=True;  AttachDbFilename=|DataDirectory|DemoContext.mdf providerName=System.Data.SqlClient />
  </connectionStrings>
Add Model Class


public class UserCredential
    {
        [Key]
        public int ID { get; set; }
        [Required]
        public String Name { get; set; }
        public String Description { get; set; }
        public List<RolesPermission> RolesPermissions { get; set; }
    }
public class RolesPermission
    {
        [Key]
        public int RoleID { get; set; }
        [Required]
        public String RoleName { get; set; }
    }
Create Context Class
public class DemoContext: DbContext
    {
        public DemoContext()
            : base(“name=DemoContext”)
        {
        }
 public System.Data.Entity.DbSet<WebAPIOdataDemo.Models.UserCredential> UserCredentials { get; set; }
    }
Setting to default routes
public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: “DefaultApi”,
                routeTemplate: “api/{controller}/{id}”,
                defaults: new { id = RouteParameter.Optional }
            );
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<UserCredential>(“UserCredentials”);
            builder.EntitySet<RolesPermission>(“RolesPermissions”);
            config.Routes.MapODataServiceRoute(routeName: “ODataRoute”,
                routePrefix: null,
                model: builder.GetEdmModel());
    }
Add API Controller (Here I am using OdataV3)


Context Selection 



Configure the OData Endpoint
Open the file App_Start/WebApiConfig.cs. Add the following using statements
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
            builder.EntitySet<UserCredential>(“UserCredentials”);
            builder.EntitySet<RolesPermission>(“RolesPermissions”);
            config.Routes.MapODataServiceRoute(routeName: “ODataRoute”,
                routePrefix: null,
                model: builder.GetEdmModel());
Run the solution
Here is output 



   

You Might Also Like...

No Comments

    Leave a Reply