Table of Contents

Getting started

This guide takes you from an empty project to a running, queryable OData API in about ten minutes. It uses EF Core's in-memory provider, so there is no database server to install — yet the data access is genuinely async, exactly what you would write against a real database. To watch those same queries turn into real SQL, follow the EF Core + SQLite tutorial next.

Prerequisites

  • The .NET 10 SDK (OhData targets net8.0 and net10.0).
  • Any editor — the whole API is plain C#.

1. Create a project and add the packages

dotnet new web -o ShopApi
cd ShopApi
dotnet add package EnGen.OhData.AspNetCore
dotnet add package Microsoft.EntityFrameworkCore.InMemory

2. Define an entity

Your model is an ordinary CLR class. Nothing OData-specific here.

// Product.cs
namespace ShopApi;

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; } = "";
    public decimal Price { get; set; }
}

3. Define a DbContext

OhData reads and writes through your EF Core DbContext — the profile in the next step injects it. This one exposes a single Products set and seeds a few rows so there is something to query.

// AppDbContext.cs
using Microsoft.EntityFrameworkCore;

namespace ShopApi;

public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(options)
{
    public DbSet<Product> Products => Set<Product>();

    protected override void OnModelCreating(ModelBuilder modelBuilder) =>
        modelBuilder.Entity<Product>().HasData(
            new Product { Id = 1, Name = "Claw Hammer",      Price = 14.99m },
            new Product { Id = 2, Name = "Adjustable Wrench", Price = 12.25m },
            new Product { Id = 3, Name = "Cordless Drill",    Price = 89.00m });
}

4. Define an entity set profile

A profile is the entire API surface for one entity set. It derives from EntitySetProfile<TKey, TModel>, passes a key selector to the base constructor, and assigns handler delegates in its constructor. Only the handlers you assign become routes — this is OhData's headline rule. Assign GetQueryable and you get a queryable collection; leave Delete unassigned and there is no DELETE route at all.

The profile injects AppDbContext through its constructor. Profiles are registered scoped, so each request gets its own profile instance holding that request's DbContext — the ordinary ASP.NET Core lifetime, no extra plumbing.

// ProductProfile.cs
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using OhData;

namespace ShopApi;

public class ProductProfile : EntitySetProfile<int, Product>
{
    public ProductProfile(AppDbContext db) : base(x => x.Id)
    {
        // Query capabilities are OFF by default and enforced at runtime: a request that uses a
        // disabled option gets a 400, not a silently-ignored parameter. Opt into the ones you
        // want to support.
        FilterEnabled  = true;
        OrderByEnabled = true;
        SelectEnabled  = true;
        CountEnabled   = true;

        // Collection GET. GetQueryable hands the framework an un-materialized IQueryable, so it can
        // compose $filter/$orderby/$skip/$top onto it; EF Core then materializes the result
        // asynchronously (over a real database provider that becomes one SQL query). Returning the
        // IQueryable is itself synchronous — that is why this single handler stays Success
        // while the reads and writes below are genuinely async.
        GetQueryable = () => db.Products;

        // Single entity GET → GET /odata/Products({key}). Returns null (→ 404) when the row is absent.
        GetById = async (id, ct) =>
            OhDataResult.Success<Product?>(await db.Products.FirstOrDefaultAsync(p => p.Id == id, ct));

        // Create → POST /odata/Products. Add the row and let the database assign its key.
        Post = async (product, ct) =>
        {
            db.Products.Add(product);
            await db.SaveChangesAsync(ct);
            return OhDataResult.Success(product);
        };

        // Delete → DELETE /odata/Products({key}). Return false when the row is absent;
        // IdempotentDelete defaults to true, so a missing row yields 204 (not 404).
        Delete = async (id, ct) =>
        {
            Product? existing = await db.Products.FirstOrDefaultAsync(p => p.Id == id, ct);
            if (existing is null) return OhDataResult.Success(false);
            db.Products.Remove(existing);
            await db.SaveChangesAsync(ct);
            return OhDataResult.Success(true);
        };
    }
}

5. Register and map OhData

Three moves now: register the DbContext, then AddOhData (collects your profiles and prefix at DI-build time) and MapOhData (registers the routes after app.Build()).

AddOhData and MapOhData live in the framework's Microsoft.Extensions.DependencyInjection and Microsoft.AspNetCore.Builder namespaces, so no extra using is needed for them.

// Program.cs
using Microsoft.EntityFrameworkCore;
using ShopApi;

var builder = WebApplication.CreateBuilder(args);

// EF Core's in-memory provider — no database server to install. The SQLite tutorial swaps this
// one line for real SQLite. Profiles are scoped, so injecting this scoped context is safe.
builder.Services.AddDbContext<AppDbContext>(o => o.UseInMemoryDatabase("Shop"));

builder.Services.AddOhData(o => o
    .WithPrefix("/odata")
    .AddEntitySetProfile<ProductProfile>());
    // ...or scan an assembly: .AddProfilesFromAssemblyOf<ProductProfile>()

var app = builder.Build();

// Materialize the in-memory store and apply the seed rows declared in AppDbContext.
using (var scope = app.Services.CreateScope())
{
    scope.ServiceProvider.GetRequiredService<AppDbContext>().Database.EnsureCreated();
}

app.MapOhData();

app.Run();

6. Run and query it

dotnet run

Then exercise the OData surface (adjust the port to what dotnet run prints):

# Service document and the CSDL metadata
curl "http://localhost:5000/odata/"
curl "http://localhost:5000/odata/\$metadata"

# The collection, plus query options (percent-encode spaces as %20). Property names in
# $filter/$orderby/$select are the canonical PascalCase names from $metadata; matching is
# case-insensitive, but the responses come back PascalCase, so prefer the canonical form.
curl "http://localhost:5000/odata/Products"
curl "http://localhost:5000/odata/Products?\$filter=Price%20gt%2013&\$orderby=Name"
curl "http://localhost:5000/odata/Products?\$select=Name,Price&\$top=2"
curl "http://localhost:5000/odata/Products/\$count"

# A single entity
curl "http://localhost:5000/odata/Products(1)"

# Create one (request bodies bind case-insensitively, so either casing is accepted)
curl -X POST "http://localhost:5000/odata/Products" \
     -H "Content-Type: application/json" \
     -d '{"Name":"Stud Finder","Price":29.99}'

The created row comes back in the canonical PascalCase shape that matches $metadata, with the key the database assigned:

{ "@odata.context": "http://localhost:5000/odata/$metadata#Products/$entity", "Id": 4, "Name": "Stud Finder", "Price": 29.99 }

Because you assigned GetQueryable, GetById, Post, and Delete — but not Put or Patch — those four routes exist and the update routes don't. That is the whole model: the profile is the contract.

Beyond the basics

The rest of the surface rides other profile declarations - navigation properties (HasMany/HasOptional/HasRequired), UseETag, and BindFunction/BindAction - rather than the plain CRUD handlers of step 4. Each declaration registers its routes; the trailing comments show what you get:

public class OrdersProfile : EntitySetProfile<int, Order>
{
    public OrdersProfile(AppDbContext db) : base(x => x.Id)
    {
        GetQueryable = () => db.Orders;

        // Collection navigation. getAll gives the read routes; every parameter after it is
        // OPTIONAL - supply only the ones whose route you want:
        HasMany(
            navigation: x => x.Lines,
            getAll:    async (orderId, ct) => await db.Lines.Where(l => l.OrderId == orderId).ToListAsync(ct),
                                          // GET /Orders({key})/Lines  (+ /Lines/$count)
            post:      (orderId, line, ct) => /* … */,   // optional → POST /Orders({key})/Lines  (create a related entity)
            addRef:    (orderId, lineId, ct) => /* … */, // optional → POST/PUT /Orders({key})/Lines/$ref  (link existing)
            removeRef: (orderId, lineId, ct) => /* … */, // optional → DELETE   /Orders({key})/Lines/$ref  (unlink)
            refTargetEntitySet: "Lines");                // optional → $ref routes emit @odata.id links

        // Single-valued navigation → GET /Orders({key})/Customer.
        HasOptional(
            navigation: x => x.Customer,
            get: async (orderId, ct) =>
                await db.Orders.Where(o => o.Id == orderId).Select(o => o.Customer).FirstOrDefaultAsync(ct));

        // ETag response header + If-Match concurrency on GET/PUT/PATCH/DELETE.
        UseETag(x => x.RowVersion);

        // Bound operations become routes. The entity-bound pair takes the key as its first parameter.
        BindFunction(Discounted);       // GET  /Orders/Discounted?minOff=…
        BindAction(Archive);            // POST /Orders/Archive
        BindEntityFunction(Total);      // GET  /Orders({key})/Total
        BindEntityAction(Approve);      // POST /Orders({key})/Approve
    }

    static Task<IEnumerable<Order>> Discounted(decimal minOff) => /* … */;
    static Task Archive() => /* … */;
    static Task<decimal> Total(int key) => /* … */;          // first parameter is the entity key
    static Task Approve(int key, string note) => /* … */;    // first parameter is the entity key
}

HasMany(x => x.Lines) on its own — with no handlers — registers no routes at all; it just declares the navigation for $metadata and $expand. The same optional-parameter pattern applies to HasOptional/HasRequired.

See navigation routing & $ref, property access, deep insert, and bound functions & actions for the full details behind each declaration.

And to shrink the surface instead of growing it: Ignore(x => x.CostBasis) hides a property from $metadata, query options, routes, and every request/response body — without touching the CLR model. One exception: overriding AdvancedConfigure takes the EDM out of OhData's hands, which drops Ignore()'s EDM half — the property is back in $metadata and query-addressable, though still absent from every response body. MapOhData() emits a startup Warning naming each affected property and the one-line remedy. See ignoring properties.

Where to next

  • EF Core + SQLite tutorial — swap the in-memory provider for a real relational database and watch OData query options become SQL.
  • Query options — the full $filter/$orderby/$select/$expand/$count/$search surface and the capability flags that gate it.
  • Architecture — how profiles become routes, and the design decisions behind it.
  • DTOs and EF entities — serve an API model that differs from the entity, with the query still pushed into SQL.
  • Authorization — per-operation and instance-level rules over your existing ASP.NET Core policies.