OData Client Code Generator

Leave a comment

The OData v4 Client Code Generator is an extension that can be used to generate OData code for use in applications.

To install it, open Visual Studio 2015 and under Visual Studio Gallery, search for OData Client Code Generator:

Click to Install:

The extension is now installed:

To use the extension, we will create a new Windows Console project:

Add a new item and under Code select OData Client:

Files are created in the project – a .tt file and a .ttinclude file:

Note the MetadataDocumentUri is empty:

Set this to an OData URI, for example:

public const string MetadataDocumentUri = "http://services.odata.org/V4/Northwind/Northwind.svc/";

Now, right click on ODataClient.tt and select Run Custom Tool:

You should see no errors. A cs file is generated, in this case ODataClient1.cs:

Now you can use the code that has been generated. We will call the code by creating an instance of the NorthwindEntities class pointing to our URI and then querying all products:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ODataConsoleApp.NorthwindModel;
 
namespace ODataConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            ODataWebExperimental.Northwind.Model.NorthwindEntities nw = 
                        new ODataWebExperimental.Northwind.Model.NorthwindEntities(new Uri("http://services.odata.org/V4/Northwind/Northwind.svc/"));
            var products = nw.Products.ToList();
            foreach (var product in products)
            {
                Console.WriteLine("Product Name: {0}", product.ProductName);
            }
            Console.ReadLine();
        }
    }
}

We get the following output:

 

THANKS FOR READING. BEFORE YOU LEAVE, I NEED YOUR HELP.
 

I AM SPENDING MORE TIME THESE DAYS CREATING YOUTUBE VIDEOS TO HELP PEOPLE LEARN THE MICROSOFT POWER PLATFORM.

IF YOU WOULD LIKE TO SEE HOW I BUILD APPS, OR FIND SOMETHING USEFUL READING MY BLOG, I WOULD REALLY APPRECIATE YOU SUBSCRIBING TO MY YOUTUBE CHANNEL.

THANK YOU, AND LET'S KEEP LEARNING TOGETHER.

CARL

https://www.youtube.com/carldesouza

 

ABOUT CARL DE SOUZA

Carl de Souza is a developer and architect focusing on Microsoft Dynamics 365, Power BI, Azure, and AI.

carldesouza.comLinkedIn Twitter | YouTube

 

Leave a Reply

Your email address will not be published. Required fields are marked *