Web API OData V4 Using a Singleton Part 7

This post is part 7 of the Web API and OData V4 series. This article demonstrates how to use an OData singleton with Web API. The singleton class has a list of child entities which is used to select contained entities from the SQLite database.

Part 1 Getting started with Web API and OData V4 Part 1.
Part 2 Web API and OData V4 Queries, Functions and Attribute Routing Part 2
Part 3 Web API and OData V4 CRUD and Actions Part 3
Part 4 Web API OData V4 Using enum with Functions and Entities Part 4
Part 5 Web API OData V4 Using Unity IoC, SQLite with EF6 and OData Model Aliasing Part 5
Part 6 Web API OData V4 Using Contained Models Part 6
Part 7 Web API OData V4 Using a Singleton Part 7
Part 8 Web API OData V4 Using an OData T4 generated client Part 8
Part 9 Web API OData V4 Caching Part 9
Part 10 Web API OData V4 Batching Part 10
Part 11 Web API OData V4 Keys, Composite Keys and Functions Part 11

Code: https://github.com/damienbod/WebAPIODataV4SQLite

The singleton entity is defined as follows:

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.OData.Builder;

namespace WebAPIODataV4SQLite.DomainModel
    public class SkillLevels
        public int Id { get; set; }

        public List<SkillLevel> Levels { get; set; }

    public class SkillLevel
        public int Level { get; set; }

        public string Description { get; set; }

        public virtual PlayerStats PlayerStats { get; set; }

The SkillLevels will be the OData singleton. This entity has a list of contained SkillLevel entities. The SkillLevel entity has a Contained PlayerStats list which is selected from the database.

Now the OData singleton can be defined in the model:

SingletonConfiguration<SkillLevels> skillLevels = builder.Singleton<SkillLevels>("SkillLevels");

The OData model can then be made accessible using an ODataController. The SkillLevelsController makes it possible to select the singleton or the child Levels or the PlayerStats pro level:

using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query;
using System.Web.OData.Routing;
using WebAPIODataV4SQLite.DomainModel;

namespace WebAPIODataV4SQLite.Controllers

    public class SkillLevelsController : ODataController
        private readonly SqliteContext _sqliteContext;

        public SkillLevelsController(SqliteContext sqliteContext)
            _sqliteContext = sqliteContext;

        [EnableQuery(PageSize = 20)]
        public IHttpActionResult Get()
            return Ok(GetFixedSkillLevels());

        [EnableQuery(PageSize = 20)]
        public IHttpActionResult GetPlayerStats([FromODataUri] int key)
            return Ok(GetFixedSkillLevels().Levels.FirstOrDefault(t => t.Level == key));
        [EnableQuery(PageSize = 20, AllowedQueryOptions = AllowedQueryOptions.All)]
        public IHttpActionResult GetPlayserStats([FromODataUri] int key)
            return Ok(_sqliteContext.PlayerStatsEntities.Where(t => t.SkillLevel == key));

        private SkillLevels GetFixedSkillLevels()
            return new SkillLevels
                Id =1,
                Levels = new List<SkillLevel>
                    new SkillLevel {Description = "Legend", Level = 1},
                    new SkillLevel {Description = "Master", Level = 2},
                    new SkillLevel {Description = "Senior", Level = 3},
                    new SkillLevel {Description = "Intermediate", Level = 4},
                    new SkillLevel {Description = "Junior", Level = 5},
                    new SkillLevel {Description = "Novice", Level = 6}

Now the application can be tested:






If you want to check or view the metadata, you can query as follows:


Web API OData V4 makes it very easy to use or implement OData singleton models. This can be mapped, referenced like EntitySets or Collections of EntitySets.






  1. Hi Thanks for putting my site on your blog.

    1. No problem, thanks for your blog
      Greetings Damien

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: