Introduction
Generally, entity can be accessible only when it is encapsulated inside an entity set. OData V4 provides two options to access this entity.
- Containment
- Singleton
In the previous article, we have learned about Containment. In this article, we will learn what is singleton, how to create singleton in OData.
Singleton is introduced in OData v4 and it allows defining an entity which can be addressed by its name from the service root. The simple definition of singleton is "The element represents a single entity in an entity model is called singleton". Small effort is required for service to support singleton. Singleton is very similar to EntitySet. OData internally use CreateODataEntryWriter for creates singleton and CreateODataFeedWriter used for writing EntitySet. This is biggest difference between singleton and EntitySet.
Example
Airport has many Flights. The following figure shows Entity Relation and we will use this model as data model in this example.

Here we have two entity sets: Airport and Flights. Airport entity has airport information like id , name and Flights entity have flights information like flight number, departure/destination city name etc. EntitySet may have many entities, but here we have required only entity for airport. Before the release OData v4, we have to define both entities as an EntitySet, So old design forced you to collection of entities even if you required only one entity. Here I am defining Airport entity as singleton. We have two benefits to define Airport as a singleton.
- We can directly access airport from service root
- This approach looksvery straightforward because singleton is designed to present a special entity
Define Data Model
- public partial class Flights
- {
- [Key]
- public int Id
- {
- get;
- set;
- }
- public string FlightNumber
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public string DepartureCityName
- {
- get;
- set;
- }
- public string DestinationCityName
- {
- get;
- set;
- }
- [Singleton]
- public Airport Airport
- {
- get;
- set;
- }
- }
- public partial class Airport
- {
- [Key]
- public int Id
- {
- get;
- set;
- }
- public string Name
- {
- get;
- set;
- }
- public List < Flights > Flights
- {
- get;
- set;
- }
- }



Pankaj Kumar ChoudharyPosted May 16, 2016, 11:36 PM
Nice Information Sir.......
Sonu ChaudharyPosted May 16, 2016, 3:19 PM
good one...
Debasis SahaPosted May 16, 2016, 1:51 PM
Nice One..
Neeraj KumarPosted May 16, 2016, 1:16 PM
Good
Prasanna MuraliPosted May 16, 2016, 9:54 AM
Nice one...