Web API OData V4 Using an OData T4 generated client Part 8

This article demonstrates how to use an OData generated client. The OData client is generated using a T4 template. This T4 template generator is a NuGet package which can be added to your Visual Studio. The generated C# classes from the T4 template have some drawbacks per default, but you can fulfill your standard requirements using this without any changes. If required you can edit or change the template. This post is part 8 of the Web API and OData V4 series.

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

Setting up Visual Studio

Before you can use the OData Client Generator, it needs to be installed or updated in your Visual Studio. In the tools menu click Extensions and Updates and install the OData v4 client generator package.

Now that the package is installed, add an OData T4 client:

Add the following NuGet packages to the client:

The client will use the OData service from the previous blog. This has one small change in the OData Model. The ContainerName is set to “SqliteContext”. This will be used in the OData client. Start the OData Service. The service used in this demo is accessible with the Url: http://localhost:59145/odata/$metadata

public static IEdmModel GetModel()
 ODataModelBuilder builder = new ODataConventionModelBuilder();
 builder.ContainerName = "SqliteContext";


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

The metadata can be viewed in your browser or fiddler:

Now that the service is running, go back to the OData client. In the T4 template, add the URL to the MetadataDocumentUri member.

public const string MetadataDocumentUri = "http://localhost:59145/odata/$metadata";

Right click the OData T4 file, and click Run custom Tool in the context menu. This creates the C# file from the T4 template. Now start the application and everything should work.

Here is example of some selects on the Entities and also the singleton.

var context = new SqliteContext(new Uri("http://localhost:59145/odata/"));

// Call some basic Get
var eventDataItems = context.EventData.ToList();
var animalsItems = context.AnimalType.ToList();

// This is the singleton object
var skillLevels = context.SkillLevels.Expand("Levels").GetValue();
var players = context.Player.Expand(c => c.PlayerStats).Where(u => u.PlayerStats.SkillLevel == 2).ToList();

Here’s is an example of a Entity Create and Entity Update:

// Create a new entity
var newObjectEventData = new EventData
 AnimalTypeId = animalsItems.First().Key,
 Factor = 56,
 FixChange = 13.0,
 StringTestId = "testdatafromodataclient",
 AnimalType = animalsItems.First()

// Update a new entity
var dataToUpdate = eventDataItems.FirstOrDefault();
dataToUpdate.Factor = 98;
dataToUpdate.FixChange = 97;

context.AddAndUpdateResponsePreference = DataServiceResponsePreference.IncludeContent;

// Add the data to the server
DataServiceResponse response = context.SaveChanges(SaveChangesOptions.ReplaceOnUpdate);

The results can then be checked in the database:

If consuming an OData service in a C# application, this client can be very useful. It is easy to use and quick to setup.














  1. Hello,
    I tried to insert an object which contains nested objects with no luck. would you please guide me in this.

  2. Hubert · · Reply

    I’m working with a big edmx file, to create the model the proxy read the XML file and it takes some seconds,file is arround 20MB, any idea where i can filter the entities to decrease the size of this file.

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 )

Google photo

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

Twitter picture

You are commenting using your Twitter 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: