omy.Utils.OData.Generators
0.0.1
dotnet add package omy.Utils.OData.Generators --version 0.0.1
NuGet\Install-Package omy.Utils.OData.Generators -Version 0.0.1
<PackageReference Include="omy.Utils.OData.Generators" Version="0.0.1" />
<PackageVersion Include="omy.Utils.OData.Generators" Version="0.0.1" />
<PackageReference Include="omy.Utils.OData.Generators" />
paket add omy.Utils.OData.Generators --version 0.0.1
#r "nuget: omy.Utils.OData.Generators, 0.0.1"
#:package omy.Utils.OData.Generators@0.0.1
#addin nuget:?package=omy.Utils.OData.Generators&version=0.0.1
#tool nuget:?package=omy.Utils.OData.Generators&version=0.0.1
Utils.OData.Generators
Utils.OData.Generators offers a Roslyn source generator that materializes entity classes and helpers for contexts derived from Utils.OData.ODataContext. It targets .NET 9 and consumes EDMX metadata so OData payloads can be accessed through strongly typed models.
Features
- Locates partial classes inheriting from
ODataContextand inspects their base constructor calls. - Loads EDMX definitions from relative paths or HTTP/HTTPS endpoints at build time.
- Generates nested entity classes with nullable support, XML documentation, and
[Key]attributes for primary keys. - Reports diagnostics when metadata cannot be found, downloaded, or deserialized (
ODATA001–ODATA003). - Produces deterministic source files that play nicely with incremental builds and IDE navigation.
Prerequisites
- Reference
Utils.ODatain your project. - Add
Utils.OData.Generatorsas an analyzer reference so MSBuild executes the generator. - Create a partial context that calls an
ODataContextbase constructor with the EDMX location.
Usage example
using Utils.OData;
public partial class ProductContext : ODataContext
{
public ProductContext() : base("metadata/Products.edmx") { }
}
When the project is built, the generator reads metadata/Products.edmx, produces partial classes such as ProductContext.Product, and populates each property with the correct .NET type. The generated members can then be consumed through the LINQ provider:
var context = new ProductContext();
var query = context.Products.Where(p => p.Price > 5).OrderBy(p => p.Name);
If the EDMX path is wrong or the file cannot be parsed, the generator raises diagnostics so the issue is caught during compilation instead of at runtime.
Examples
Extending generated entities
Entities are emitted as partial classes, allowing domain-specific behavior to be added alongside the generated members.
public partial class ProductContext
{
public partial class Product
{
public bool IsLowStock => UnitsInStock < 5;
}
}
Because Product is a partial class, additional properties and helper methods seamlessly coexist with generated properties while keeping custom logic separate from tooling output.
Consuming remote metadata
You can point the base constructor to a remote EDMX URL; the generator downloads and caches the metadata during build.
public partial class RemoteContext : ODataContext
{
public RemoteContext() : base("https://example.com/$metadata") { }
}
When network retrieval fails, diagnostic ODATA002 highlights the HTTP issue, while malformed payloads trigger ODATA003, making problems explicit to developers before deployment.
Handling multiple models
Large solutions often require multiple contexts. Simply declare additional partial classes, each referencing a different EDMX location, and the generator will emit isolated entity hierarchies per context.
public partial class SalesContext : ODataContext
{
public SalesContext() : base("metadata/Sales.edmx") { }
}
public partial class SupportContext : ODataContext
{
public SupportContext() : base("metadata/Support.edmx") { }
}
Each context receives its own ODataQueryable<T> properties and entity types, ensuring models stay isolated even when they share the same assembly.
Navigation properties and expansions
Generated entities include navigation properties mapped from the EDMX metadata.
using System.Linq;
using Utils.OData;
public partial class ProductContext : ODataContext
{
public ProductContext() : base("metadata/Products.edmx") { }
}
var context = new ProductContext();
var query = context.Products
.Where(p => p.Supplier.City == "Paris")
.Expand(p => p.Supplier)
.Select(p => new { p.Name, Supplier = p.Supplier.Name });
The LINQ provider recognizes the generated navigation properties and emits $expand segments
automatically when needed.
Partial classes for validation
Because entities are partial classes, domain validation rules can live alongside generated members without being lost during rebuilds.
using System;
public partial class ProductContext
{
public partial class Product
{
partial void OnNameChanging(string? value)
{
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("A product requires a non-empty name.");
}
}
}
}
The generator respects existing partial methods and only emits missing declarations, so custom validation hooks run whenever the entity property changes.
Diagnostics reference
Diagnostics surfaced by the generator can be acted upon quickly during development:
| Id | Message | Resolution hint |
|---|---|---|
| ODATA001 | ODataContext constructor did not specify a metadata source. |
Ensure the base call references a local file or URL. |
| ODATA002 | Metadata file or endpoint could not be reached. | Verify network connectivity and credentials. |
| ODATA003 | Metadata content could not be parsed into EDMX structures. | Validate that the EDMX document is well-formed XML. |
| ODATA004 | Duplicate entity names detected across merged EDMX documents. | Rename the entities or split them into distinct models. |
Keeping this table handy speeds up troubleshooting when metadata moves or services change.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.0.1 | 216 | 11/3/2025 |