Introduction to WCF Data service and ODATA
- ODATA is HTTP or web protocol to querying or manipulating data.
- ODATA uses HTTP, JOSN or ATOM.
- ODATA can be implemented through any platform supports, HTTP, XML or JSON.
- ODATA can expose data/information from relational database, File systems, Web sites, services etc.
- ODATA specification available under Microsoft’s Open Specification Promise (OSP) so third parties, including open source projects, can build Open Data Protocol clients and services.
- WCF Data service framework provides an API that allows data to be created and consumed over HTTP using RESTful service.
- WCF Data service supports all database operations using URI.
- WCF Data service can expose an entity model via an URI.
- WCF Data service is RESTful service to support CRUD operations on database.
- WCF Data service could be consumed by any type of client like Windows, SilverLight, Web, AJAX and console.
- Create a Data Access layer using ADO.Net Entity model or LINQ.
- Make sure Data model is implementing IQueryable and IUpdateable .
- Expose the data model as REST service using WCF Data service.
- Using ADO.Net Entity model.
- Using LINQ to SQL class.
- Custom Data Model.
- Right click on web application and add a new item
- Select ADO.Net Entity model from Data tab.
- Since we have table in data base. so we are going to choose option , select from database.
- Either choose the data base from drop down or create a new data connection.
In above connection string StudentDBEntities is name of the connection string. If we want, we can change this connection string as per our requirement. If your required data base is not listed in drop down then, you can create a new data connection. To create new data connection click on New Connection.
You can give the data base server name and press refresh. After pressing Refresh, you can choose the data base from the drop down. After selecting the data base click on Test Connection to test connection established successfully or not?
- Select tables, views and stored procedure from data base you want to make as the part of your data model. Since we are having only one table so we are selecting one table.
If you want you can change name of the data model. By default it is name of the data base appended by the term model. Click on Finish button to complete and create the data model.
- Now we can see that StudentDataModel.edmx has been created in the designer.
Since there is only one table, so there is only one table model at design surface.
Now we have created the data model which can be exposed as WCF Data Service. Now you can see in solution explorer, you have StudentModel.edmx and StdentModel.Designer.cs files.
- Right click on Web Application project and add a new item.
- Select WCF Data Service from Web tab. Give any meaningful name. I am leaving the default name here.
- After adding the WCF Data Service, we can see a service file with extension .svc has been added to the solution explorer. When we click on .svc.cs file, we can see the code generated for us.
Very first we need to put data source name. To do so uncomment the first commented line and put the data source name. In our case name of the model, which we created in 2nd step is the data source. Our data source name is StudentDBEntities
Now we need to set access rules for entity or entity set. Since we have only one table, so either we can give name of the table explicitly or if we want to set the same access rule for all the table in the data model or data source we could put *.
- One feature of WCF data service is that, they provide operation to retrieve Metadata about the service and offer of the service.
- This is very useful in determining structure before requesting them.
- WCF Data service provides metadata accessible.
- Metadata expose all the resource and custom services.
- Metadata could be accessed by appending $metadata to service URI.
|expand||Request set of related entities to be retrieved.|
|orderby||Indicates the sequence entity to be retrieved|
|skip||Skip number of data items.|
|top||At most returns top number of data items|
|filter||Applies the filtering condition|
|ge||Greate than or equal to|
|le||Less than or equal to|
- Fetch all the records from the table Student
- Fetch the student details with roll number 1
- Fetch top 2 record from the student table
- Fetch the records of the student with roll number 1 or 3
URI: http://localhost:29409/WcfDataService1.svc/Students?($filter=RollNumber eq’1′)or($filter=rollnumber eq ‘3’)
|All||All Read and Write are permitted|
|AllRead||All Read permitted|
|AllWrite||All Write is permitted|
|None||No access is permitted|
|ReadMultiple||Reading multiple row is permitted|
|ReadSingle||Reading a single row is permitted|
|WriteAppend||Creating New data is permitted|
|WriteDelete||Deleting data is permitted|
|WriteMerge||Merge updating is permitted|
|WriteReplace||Replace updating is permitted|