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.
odataClientData_02

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

Add the following NuGet packages to the client:
odataClientData_04

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";
 builder.EntitySet<AnimalType>("AnimalType");
 builder.EntitySet<EventData>("EventData");

 builder.EntitySet<Player>("Player");
 builder.EntityType<PlayerStats>();

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

The metadata can be viewed in your browser or fiddler:
odataClientData_05

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/"));
context.Format.UseJson();

// 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.AddToEventData(newObjectEventData);
context.UpdateObject(dataToUpdate);
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:
odataClientData_06

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

Links:

http://blogs.msdn.com/b/odatateam/archive/2014/03/11/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx

http://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v4/

http://www.asp.net/web-api/overview/releases/whats-new-in-aspnet-web-api-22#OData

http://visualstudiogallery.msdn.microsoft.com/9b786c0e-79d1-4a50-89a5-125e57475937

http://www.odata.org/libraries/

http://wp.sjkp.dk/generate-c-classes-from-odata-metadata/

http://odata.jenspinney.com/

http://blogs.msdn.com/b/mrtechnocal/archive/2013/11/08/odata-web-api-batching-with-actions.aspx

http://blogs.msdn.com/b/odatateam/archive/2014/07/09/odata-client-code-generator-2-0-0-release.aspx

http://blogs.msdn.com/b/odatateam/archive/2014/07/09/client-delayed-query.aspx

http://blogs.msdn.com/b/webdev/archive/2013/11/01/introducing-batch-support-in-web-api-and-web-api-odata.aspx

3 comments

  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

    Hello,
    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 comment

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