Skip to content

Instantly share code, notes, and snippets.

@MatthewDavidCampbell
Last active August 8, 2022 13:03
Show Gist options
  • Save MatthewDavidCampbell/7f0433966742fb4ffd05a6f24a3df18b to your computer and use it in GitHub Desktop.
Save MatthewDavidCampbell/7f0433966742fb4ffd05a6f24a3df18b to your computer and use it in GitHub Desktop.
Extends OData client context (DataServiceContext)
using Microsoft.OData.Client;
using Microsoft.OData.Edm;
using System;
namespace Biometria.Discovery.OData
{
public class AzureSearchContext : DataServiceContext
{
public AzureSearchContext(string url) : base(new Uri(url))
{
Actors = base.CreateQuery<Actor>("aktor/docs");
Format.LoadServiceModel = () =>
{
EdmEntityType actor = new EdmEntityType(typeof(Actor).Namespace, nameof(Actor));
EdmStructuralProperty identity = actor.AddStructuralProperty(nameof(Actor.Identitet), EdmPrimitiveTypeKind.String);
actor.AddKeys(identity);
EdmModel model = new EdmModel();
model.AddElement(actor);
return model;
};
Format.UseJson();
Configurations.RequestPipeline.OnMessageCreating = (args) => new AzureHttpClientRequestMessage(args);
}
public DataServiceQuery<Actor> Actors { get; }
}
}
@MatthewDavidCampbell
Copy link
Author

MatthewDavidCampbell commented Aug 8, 2022

Edm model has to be built explicitly otherwise the search query properties are not correctly defined. Mapping from Azure Search index definition in JSON to EDM XML probably the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment