Class ODataContext
Provides a base class for generated OData contexts. The context is initialized from an EDMX definition provided as a stream or a path.
public abstract class ODataContext
- Inheritance
-
ODataContext
- Inherited Members
- Extension Methods
Remarks
The synchronous constructors are retained for backward compatibility with generated
code. They perform blocking I/O and, for remote URLs, block a thread on a network call
(item 14 / item 29). New code should prefer the asynchronous CreateAsync and
LoadMetadataAsync(string, ODataMetadataOptions?, CancellationToken) factory
methods, which accept a caller-supplied HttpClient, honour a
CancellationToken, and apply a consistent bounded size policy to every metadata
source (items 15, 16, 33, 34).
Constructors
ODataContext(Stream)
Initializes a new instance of the ODataContext class from the specified EDMX stream.
protected ODataContext(Stream edmxStream)
Parameters
edmxStreamStreamStream containing the EDMX metadata to load.
Exceptions
- ArgumentNullException
Thrown when
edmxStreamis null.- InvalidOperationException
Thrown when the EDMX metadata cannot be parsed.
ODataContext(string)
Initializes a new instance of the ODataContext class from the specified file or URL.
protected ODataContext(string edmxPathOrUrl)
Parameters
edmxPathOrUrlstringPath or HTTP/HTTPS URL pointing to an EDMX file.
Exceptions
- ArgumentException
Thrown when
edmxPathOrUrlis null or empty.- InvalidOperationException
Thrown when the EDMX metadata cannot be read or parsed.
ODataContext(Edmx)
Initializes a new instance of the ODataContext class from already-parsed metadata. This constructor performs no I/O and is used by the asynchronous factory methods.
protected ODataContext(Edmx metadata)
Parameters
metadataEdmxThe parsed EDMX metadata.
Exceptions
- ArgumentNullException
Thrown when
metadatais null.
Properties
Metadata
Gets the EDMX metadata loaded for the current context.
public Edmx Metadata { get; }
Property Value
Methods
CreateAsync<TContext>(Stream, Func<Edmx, TContext>, ODataMetadataOptions?, CancellationToken)
Asynchronously creates a context instance from a caller-provided EDMX stream, delegating parsing
to LoadMetadataFromStreamAsync(Stream, ODataMetadataOptions?, CancellationToken) and
then constructing TContext through the supplied factory.
public static Task<TContext> CreateAsync<TContext>(Stream edmxStream, Func<Edmx, TContext> factory, ODataMetadataOptions? options = null, CancellationToken cancellationToken = default) where TContext : ODataContext
Parameters
edmxStreamStreamStream containing the EDMX content.
factoryFunc<Edmx, TContext>Factory that builds the context from the parsed metadata.
optionsODataMetadataOptionscancellationTokenCancellationTokenToken used to cancel the load operation.
Returns
- Task<TContext>
A task producing the constructed context.
Type Parameters
TContextConcrete context type to create.
Exceptions
- ArgumentNullException
Thrown when
edmxStreamorfactoryis null.
CreateAsync<TContext>(string, Func<Edmx, TContext>, ODataMetadataOptions?, CancellationToken)
Asynchronously creates a context instance from an EDMX file path or HTTP/HTTPS URL, delegating
loading to LoadMetadataAsync(string, ODataMetadataOptions?, CancellationToken) and
then constructing TContext through the supplied factory.
public static Task<TContext> CreateAsync<TContext>(string edmxPathOrUrl, Func<Edmx, TContext> factory, ODataMetadataOptions? options = null, CancellationToken cancellationToken = default) where TContext : ODataContext
Parameters
edmxPathOrUrlstringPath or HTTP/HTTPS URL pointing to an EDMX resource.
factoryFunc<Edmx, TContext>Factory that builds the context from the parsed metadata.
optionsODataMetadataOptionscancellationTokenCancellationTokenToken used to cancel the load operation.
Returns
- Task<TContext>
A task producing the constructed context.
Type Parameters
TContextConcrete context type to create.
Exceptions
- ArgumentException
Thrown when
edmxPathOrUrlis null or whitespace.- ArgumentNullException
Thrown when
factoryis null.
LoadMetadataAsync(string, ODataMetadataOptions?, CancellationToken)
Loads and parses EDMX metadata from the specified file path or HTTP/HTTPS URL without blocking the calling thread, using a caller-supplied transport and a bounded size policy.
public static Task<Edmx> LoadMetadataAsync(string edmxPathOrUrl, ODataMetadataOptions? options = null, CancellationToken cancellationToken = default)
Parameters
edmxPathOrUrlstringPath or HTTP/HTTPS URL pointing to an EDMX resource.
optionsODataMetadataOptionsOptional loading options controlling the maximum metadata size, the HttpClient to reuse, the download timeout, and the cross-host redirect policy. When null, Default is used.
cancellationTokenCancellationTokenToken used to cancel the load operation.
Returns
Exceptions
- ArgumentException
Thrown when
edmxPathOrUrlis null or whitespace.- InvalidOperationException
Thrown when the metadata cannot be read or parsed.
LoadMetadataFromStreamAsync(Stream, ODataMetadataOptions?, CancellationToken)
Loads and parses EDMX metadata from a caller-provided stream without blocking the calling thread, enforcing the configured maximum size (item 15 / item 33).
public static Task<Edmx> LoadMetadataFromStreamAsync(Stream edmxStream, ODataMetadataOptions? options = null, CancellationToken cancellationToken = default)
Parameters
edmxStreamStreamStream containing the EDMX content.
optionsODataMetadataOptionsOptional loading options controlling the maximum metadata size. When null, Default is used.
cancellationTokenCancellationTokenToken used to cancel the load operation.
Returns
Remarks
For seekable streams the EDMX document is read starting at the stream's current Position. The size limit is checked against the number of bytes remaining from that position, not the total stream length. The position is not reset before reading.
Exceptions
- ArgumentNullException
Thrown when
edmxStreamis null.- InvalidOperationException
Thrown when the metadata cannot be parsed or exceeds the size limit.
Query<TEntity>(string)
Creates a queryable sequence targeting the specified entity set.
protected ODataQueryable<TEntity> Query<TEntity>(string entitySetName)
Parameters
entitySetNamestringName of the entity set to query.
Returns
- ODataQueryable<TEntity>
An ODataQueryable<TEntity> representing the entity set.
Type Parameters
TEntityType of the entity returned by the query.
Exceptions
- ArgumentException
Thrown when
entitySetNameis null or whitespace.
Table(string)
Creates a queryable sequence for an entity set when no CLR type exists for the table.
public ODataQueryable<ODataUntypedRow> Table(string entitySetName)
Parameters
entitySetNamestringName of the entity set to query.
Returns
- ODataQueryable<ODataUntypedRow>
An untyped ODataQueryable<TEntity> that exposes ODataUntypedRow instances.
Exceptions
- ArgumentException
Thrown when
entitySetNameis null or whitespace.