In this article you will learn how to create an OData Service in ASP.Net.
First of all start Visual Studio and create a new website and provide the name and location and click "Next" then click on "Add new item" and select "ADO.NET Entity Data Model" from installed templates and click "Add".
Image 1.
And the next step is to choose "Generate from database" and click the "Next" button and after that choose your data connection.
Image 2.
Now select table names and objects and click "Finish".
Image 3.
Now right-click on the solution and click on "Add new item" and select "WCF Data Service".
Image 4.
This is the default class code.
public class CustomersService : DataService< /* TODO: put your data source class name here */ >
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
// config.SetEntitySetAccessRule("MyEntityset", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
You need to make a few changes like the following:
public class CustomersService : DataService<NORTHWNDEntities1>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("Customers", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
}
Now right-click on the service and click on "View in browser".
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<service xml:base="http://localhost:5376/CustomersService.svc/">
<workspace>
<atom:title>Default</atom:title>
<collection href="Customers">
<atom:title>Customers</atom:title>
</collection>
</workspace>
</service>