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 




   
Create the Odata Project using Visual Studio Reviewed by Rupesh on 20:56 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.