Class EntityMetadataBuilder<T>
Fluent builder, passed to From<T>(...), that declares the table name and column
mappings of an entity type.
public class EntityMetadataBuilder<T>
Type Parameters
T
- Inheritance
-
EntityMetadataBuilder<T>
- Inherited Members
- Extension Methods
Methods
AutoBuild()
Builds the metadata entirely by reflecting over the CLR type, ignoring any table or property mappings declared on this builder. Attribute-declared relationships are still read (they are part of the CLR type); fluent relationship declarations are not.
public IEntityMetadata AutoBuild()
Returns
- IEntityMetadata
The auto-derived entity metadata.
Build()
Builds the metadata from the mappings declared on this builder, auto-deriving the table name and, when no properties were declared, the property mappings from the CLR type. Declaring even one property fluently maps only the declared properties: the CLR attributes on the other properties are not auto-built (they would otherwise need merging with the fluent overrides).
public IEntityMetadata Build()
Returns
- IEntityMetadata
The resolved entity metadata.
HasManyThrough<TChild, TJunction>(Expression<Func<T, IEnumerable<TChild>>>, Expression<Func<T, object?>>, Expression<Func<TJunction, object?>>, Expression<Func<TChild, object?>>, Expression<Func<TJunction, object?>>)
Declares a many-to-many relationship with explicit key components on both legs: a collection navigation on this entity, the parent-side key on this entity, the junction foreign key that references it, the child-side key on the related type and the junction foreign key that references that. Each pair must have the same number of components and matching positional types.
public EntityMetadataBuilder<T> HasManyThrough<TChild, TJunction>(Expression<Func<T, IEnumerable<TChild>>> navigation, Expression<Func<T, object?>> parentKey, Expression<Func<TJunction, object?>> junctionParentForeignKey, Expression<Func<TChild, object?>> childKey, Expression<Func<TJunction, object?>> junctionChildForeignKey) where TChild : class where TJunction : class
Parameters
navigationExpression<Func<T, IEnumerable<TChild>>>Selects the collection navigation on this entity, for example
p => p.Children.parentKeyExpression<Func<T, object>>Selects the parent-side key components on this entity.
junctionParentForeignKeyExpression<Func<TJunction, object>>Selects the junction components that reference
parentKey.childKeyExpression<Func<TChild, object>>Selects the child-side key components on
TChild.junctionChildForeignKeyExpression<Func<TJunction, object>>Selects the junction components that reference
childKey.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe child entity type.
TJunctionThe junction (link) entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector, the navigation is not a collection of
TChild, or a key leg does not match its junction foreign key's arity or types.
HasManyThrough<TChild, TJunction>(Expression<Func<T, IEnumerable<TChild>>>, Expression<Func<TJunction, object?>>, Expression<Func<TJunction, object?>>)
Declares a many-to-many relationship with the default full principal keys on both sides: a collection navigation on this entity, the junction foreign key that references this entity's full key and the junction foreign key that references the related type's full key. Both keys are taken from entity metadata in property order.
public EntityMetadataBuilder<T> HasManyThrough<TChild, TJunction>(Expression<Func<T, IEnumerable<TChild>>> navigation, Expression<Func<TJunction, object?>> junctionParentForeignKey, Expression<Func<TJunction, object?>> junctionChildForeignKey) where TChild : class where TJunction : class
Parameters
navigationExpression<Func<T, IEnumerable<TChild>>>Selects the collection navigation on this entity, for example
p => p.Children.junctionParentForeignKeyExpression<Func<TJunction, object>>Selects the junction components that reference this entity's key.
junctionChildForeignKeyExpression<Func<TJunction, object>>Selects the junction components that reference the related type's key.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe child entity type.
TJunctionThe junction (link) entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector, the navigation is not a collection of
TChild, or a default key's arity does not match its junction foreign key.
HasManyThrough<TChild, TJunction, TParentKey, TChildKey>(Expression<Func<T, IEnumerable<TChild>>>, Expression<Func<T, TParentKey>>, Expression<Func<TJunction, TParentKey>>, Expression<Func<TChild, TChildKey>>, Expression<Func<TJunction, TChildKey>>)
Declares a many-to-many relationship: a collection navigation on this entity, the keys on both
principal sides and the junction (link) entity's foreign keys that reference them. The junction is
explicit; the core does not infer it. The navigation participates in a declared relationship and is
excluded from Properties. The keys are resolved lazily, so the junction and the child type
may be registered later.
public EntityMetadataBuilder<T> HasManyThrough<TChild, TJunction, TParentKey, TChildKey>(Expression<Func<T, IEnumerable<TChild>>> navigation, Expression<Func<T, TParentKey>> parentKey, Expression<Func<TJunction, TParentKey>> junctionParentForeignKey, Expression<Func<TChild, TChildKey>> childKey, Expression<Func<TJunction, TChildKey>> junctionChildForeignKey) where TChild : class where TJunction : class
Parameters
navigationExpression<Func<T, IEnumerable<TChild>>>Selects the collection navigation on this entity, for example
p => p.Children.parentKeyExpression<Func<T, TParentKey>>Selects the parent-side key property on this entity, for example
p => p.Id.junctionParentForeignKeyExpression<Func<TJunction, TParentKey>>Selects the junction foreign key referencing the parent key, for example
l => l.ParentId.childKeyExpression<Func<TChild, TChildKey>>Selects the child-side key property, for example
c => c.Id.junctionChildForeignKeyExpression<Func<TJunction, TChildKey>>Selects the junction foreign key referencing the child key, for example
l => l.ChildId.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe child entity type.
TJunctionThe junction (link) entity type.
TParentKeyThe property type shared by the parent key and the junction parent foreign key.
TChildKeyThe property type shared by the child key and the junction child foreign key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector does not select a property, the navigation is not a collection of
TChild, or a key does not match its junction foreign key's type.
HasMany<TChild>(Expression<Func<T, ICollection<TChild>>>, Expression<Func<TChild, object?>>, Expression<Func<T, object?>>)
Declares a one-to-many relationship with an explicit principal key: a collection navigation on this entity, the foreign-key components on the dependent type and the principal-key components on this entity.
public EntityMetadataBuilder<T> HasMany<TChild>(Expression<Func<T, ICollection<TChild>>> navigation, Expression<Func<TChild, object?>> foreignKey, Expression<Func<T, object?>> principalKey) where TChild : class
Parameters
navigationExpression<Func<T, ICollection<TChild>>>Selects the collection navigation on this entity, for example
p => p.Children.foreignKeyExpression<Func<TChild, object>>Selects the foreign-key components on
TChild, for examplec => new { c.TenantId, c.ParentId }.principalKeyExpression<Func<T, object>>Selects the principal-key components on this entity, for example
p => new { p.TenantId, p.Id }.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe dependent entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector, or the navigation is not a collection of
TChild.
HasMany<TChild>(Expression<Func<TChild, object?>>)
Declares a one-to-many relationship without a navigation, a foreign key and the default principal key: the principal key is the full key of this entity, in entity-metadata order.
public EntityMetadataBuilder<T> HasMany<TChild>(Expression<Func<TChild, object?>> foreignKey) where TChild : class
Parameters
foreignKeyExpression<Func<TChild, object>>Selects the foreign-key components on
TChild.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe dependent entity type.
Exceptions
- ArgumentNullException
foreignKeyis null.- NotSupportedException
The selector is not a supported key selector, or its arity does not match the full principal key.
HasMany<TChild>(Expression<Func<TChild, object?>>, Expression<Func<T, object?>>)
Declares a one-to-many relationship without a navigation and with an explicit principal key: the foreign-key components are selected on the dependent type and the principal-key components on this entity.
public EntityMetadataBuilder<T> HasMany<TChild>(Expression<Func<TChild, object?>> foreignKey, Expression<Func<T, object?>> principalKey) where TChild : class
Parameters
foreignKeyExpression<Func<TChild, object>>Selects the foreign-key components on
TChild.principalKeyExpression<Func<T, object>>Selects the principal-key components on this entity.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe dependent entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector.
HasMany<TChild, TKey>(Expression<Func<T, ICollection<TChild>>>, Expression<Func<TChild, TKey>>)
Declares the principal side of a one-to-many relationship: a collection navigation on this entity and the foreign-key property on the related (dependent) type. The principal key is taken from this entity's key metadata; it is resolved lazily and must exist by the time the keys are read. The selector may select a scalar property or a flat anonymous object; the components keep the selector order.
public EntityMetadataBuilder<T> HasMany<TChild, TKey>(Expression<Func<T, ICollection<TChild>>> navigation, Expression<Func<TChild, TKey>> foreignKey) where TChild : class
Parameters
navigationExpression<Func<T, ICollection<TChild>>>Selects the collection navigation on this entity, for example
p => p.Children.foreignKeyExpression<Func<TChild, TKey>>Selects the foreign-key property on
TChild, for examplec => c.ParentId.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe dependent entity type.
TKeyThe property type shared by the foreign key and the principal key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector does not select a property, or the navigation is not a collection of
TChild.
HasMany<TChild, TKey>(Expression<Func<TChild, TKey>>, Expression<Func<T, TKey>>)
Declares the principal side of a one-to-many relationship for an entity whose navigation is not modelled as a property: the foreign key is selected on the dependent type and the principal key on this entity, and no navigation is attached.
public EntityMetadataBuilder<T> HasMany<TChild, TKey>(Expression<Func<TChild, TKey>> foreignKey, Expression<Func<T, TKey>> principalKey) where TChild : class
Parameters
foreignKeyExpression<Func<TChild, TKey>>Selects the foreign-key property on
TChild, for examplec => c.ParentId.principalKeyExpression<Func<T, TKey>>Selects the principal-key property on this entity, for example
p => p.Id.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe dependent entity type.
TKeyThe property type shared by the foreign key and the principal key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector does not select a property.
HasOneToOne<TChild>(Expression<Func<T, TChild?>>, Expression<Func<T, object?>>, Expression<Func<TChild, object?>>)
Declares a one-to-one relationship with explicit key components: a reference navigation on this (principal) entity, the principal-key components on this entity and the foreign-key components on the related (dependent) type. Uniqueness of the foreign key remains a declaration-time trust; the core does not inspect the schema.
public EntityMetadataBuilder<T> HasOneToOne<TChild>(Expression<Func<T, TChild?>> navigation, Expression<Func<T, object?>> parentKey, Expression<Func<TChild, object?>> childForeignKey) where TChild : class
Parameters
navigationExpression<Func<T, TChild>>Selects the reference navigation on this entity, for example
p => p.Profile.parentKeyExpression<Func<T, object>>Selects the principal-key components on this entity, for example
p => new { p.TenantId, p.Id }.childForeignKeyExpression<Func<TChild, object>>Selects the foreign-key components on
TChild, for examplec => new { c.TenantId, c.OwnerId }.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe related (dependent) entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector, or the navigation is a value type or string.
HasOneToOne<TChild, TKey>(Expression<Func<T, TChild?>>, Expression<Func<T, TKey>>, Expression<Func<TChild, TKey>>)
Declares a one-to-one relationship: a reference navigation on this (principal) entity, the
principal key on this entity and the unique foreign key on the related (dependent) type. The
uniqueness of the foreign key is a declaration-time trust: the core neither inspects the schema
or the data nor imposes a uniqueness constraint, so a non-unique foreign key is accepted when
the metadata is built. The only runtime guard is per parent through JoinInto: two distinct
non-null child identities for one parent throw InvalidOperationException, while
repeated identical rows and keyless/null-identity repeats are tolerated (the first child wins).
This is the principal counterpart of the HasOne overload, whose foreign key lives on the
declaring type.
public EntityMetadataBuilder<T> HasOneToOne<TChild, TKey>(Expression<Func<T, TChild?>> navigation, Expression<Func<T, TKey>> parentKey, Expression<Func<TChild, TKey>> childForeignKey) where TChild : class
Parameters
navigationExpression<Func<T, TChild>>Selects the reference navigation on this entity, for example
p => p.Profile.parentKeyExpression<Func<T, TKey>>Selects the principal-key property on this entity, for example
p => p.Id.childForeignKeyExpression<Func<TChild, TKey>>Selects the foreign-key property on
TChild, for examplec => c.ParentId.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe related (dependent) entity type.
TKeyThe property type shared by the principal key and the foreign key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector does not select a property, or the navigation is a value type or string.
HasOneToOne<TChild, TKey>(Expression<Func<T, TChild?>>, Expression<Func<TChild, TKey>>)
Declares a one-to-one relationship without an explicit principal key: a reference navigation on this (principal) entity and the foreign-key components on the related (dependent) type. The principal key is the full key of this entity, in entity-metadata order.
public EntityMetadataBuilder<T> HasOneToOne<TChild, TKey>(Expression<Func<T, TChild?>> navigation, Expression<Func<TChild, TKey>> childForeignKey) where TChild : class
Parameters
navigationExpression<Func<T, TChild>>Selects the reference navigation on this entity, for example
p => p.Profile.childForeignKeyExpression<Func<TChild, TKey>>Selects the foreign-key component(s) on
TChild, for examplec => new { c.TenantId, c.OwnerId }.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe related (dependent) entity type.
TKeyThe property type of the child foreign key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector, or the navigation is a value type or string.
HasOne<TChild>(Expression<Func<T, object?>>)
Declares a many-to-one relationship without a navigation, a foreign key and the default
principal key: the principal key is the full key of TChild, in
entity-metadata order.
public EntityMetadataBuilder<T> HasOne<TChild>(Expression<Func<T, object?>> foreignKey) where TChild : class
Parameters
foreignKeyExpression<Func<T, object>>Selects the foreign-key components on this entity.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe principal entity type.
Exceptions
- ArgumentNullException
foreignKeyis null.- NotSupportedException
The selector is not a supported key selector, or its arity does not match the full principal key.
HasOne<TChild>(Expression<Func<T, object?>>, Expression<Func<TChild, object?>>)
Declares a many-to-one relationship without a navigation and with an explicit principal key: the foreign-key components are selected on this entity and the principal-key components on the related (principal) type.
public EntityMetadataBuilder<T> HasOne<TChild>(Expression<Func<T, object?>> foreignKey, Expression<Func<TChild, object?>> principalKey) where TChild : class
Parameters
foreignKeyExpression<Func<T, object>>Selects the foreign-key components on this entity.
principalKeyExpression<Func<TChild, object>>Selects the principal-key components on
TChild.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe principal entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector.
HasOne<TChild>(Expression<Func<T, TChild?>>, Expression<Func<T, object?>>, Expression<Func<TChild, object?>>)
Declares a many-to-one relationship with an explicit principal key: a reference navigation on this entity, the foreign-key components on this entity and the principal-key components on the related (principal) type.
public EntityMetadataBuilder<T> HasOne<TChild>(Expression<Func<T, TChild?>> navigation, Expression<Func<T, object?>> foreignKey, Expression<Func<TChild, object?>> principalKey) where TChild : class
Parameters
navigationExpression<Func<T, TChild>>Selects the reference navigation on this entity, for example
c => c.Parent.foreignKeyExpression<Func<T, object>>Selects the foreign-key components on this entity, for example
c => new { c.TenantId, c.ParentId }.principalKeyExpression<Func<TChild, object>>Selects the principal-key components on
TChild, for examplep => new { p.TenantId, p.Id }.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe principal entity type.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector is not a supported key selector, or the navigation is a value type or string.
HasOne<TChild, TKey>(Expression<Func<T, TChild?>>, Expression<Func<T, TKey>>)
Declares the dependent side of a many-to-one relationship: a reference navigation on this entity and the foreign-key property on this same entity. The principal key is taken from the related entity's key metadata; it is resolved lazily and must exist by the time the keys are read.
public EntityMetadataBuilder<T> HasOne<TChild, TKey>(Expression<Func<T, TChild?>> navigation, Expression<Func<T, TKey>> foreignKey) where TChild : class
Parameters
navigationExpression<Func<T, TChild>>Selects the reference navigation on this entity, for example
c => c.Parent.foreignKeyExpression<Func<T, TKey>>Selects the foreign-key property on this entity, for example
c => c.ParentId.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe principal entity type.
TKeyThe property type shared by the foreign key and the principal key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector does not select a property, or the navigation is a value type or string.
HasOne<TChild, TKey>(Expression<Func<T, TKey>>, Expression<Func<TChild, TKey>>)
Declares the dependent side of a many-to-one relationship for an entity whose navigation is not modelled as a property: the foreign key is selected on this entity and the principal key on the related type, and no navigation is attached.
public EntityMetadataBuilder<T> HasOne<TChild, TKey>(Expression<Func<T, TKey>> foreignKey, Expression<Func<TChild, TKey>> principalKey) where TChild : class
Parameters
foreignKeyExpression<Func<T, TKey>>Selects the foreign-key property on this entity, for example
c => c.ParentId.principalKeyExpression<Func<TChild, TKey>>Selects the principal-key property on
TChild, for examplep => p.Id.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Type Parameters
TChildThe principal entity type.
TKeyThe property type shared by the foreign key and the principal key.
Exceptions
- ArgumentNullException
A selector is null.
- NotSupportedException
A selector does not select a property.
HasQueryFilter(Func<EntityBuilder<T>, IDataContext, EntityBuilder<T>>)
Declares an anonymous global query filter in the builder-function form. The function receives a
builder over the entity and the executing IDataContext, and must express the filter
by calling Where; only that predicate is merged into every query in which the entity
participates. The function is invoked once while the plan is built. Values that must stay
parameterised (so the plan can be cached) have to be read from the IDataContext or
supplied through SqlFunctions.Parameter<T>(idx); snapshotting a runtime value into a
local before the Where call, or changing anything other than Where, is rejected.
A repeated anonymous call on this builder adds another filter. The mapping is registered once per
entity type: a later From<T>(...) configuration for the same type is ignored, so the
first registration wins.
public EntityMetadataBuilder<T> HasQueryFilter(Func<EntityBuilder<T>, IDataContext, EntityBuilder<T>> filter)
Parameters
filterFunc<EntityBuilder<T>, IDataContext, EntityBuilder<T>>The filter function that appends the predicate with
Where.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
HasQueryFilter(Expression<Func<T, IDataContext, bool>>)
Declares an anonymous global query filter that is applied to every query in which the entity
participates (the primary source, joins and subqueries) unless the query calls
IgnoreFilters. The predicate receives the executing IDataContext so it can
read per-context state such as a tenant identifier. A repeated anonymous call on this builder
adds another predicate; the declared anonymous filters are combined with and. The mapping
is registered once per entity type: a later From<T>(...) configuration for the same
type is ignored, so the first registration wins.
public EntityMetadataBuilder<T> HasQueryFilter(Expression<Func<T, IDataContext, bool>> filter)
Parameters
filterExpression<Func<T, IDataContext, bool>>The filter predicate over the entity and the executing data context.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
HasQueryFilter(Expression<Func<T, bool>>)
Declares an anonymous global query filter that is applied to every query in which the entity
participates (the primary source, joins and subqueries) unless the query calls
IgnoreFilters. A repeated anonymous call on this builder adds another predicate; the
declared anonymous filters are combined with and. The mapping is registered once per
entity type: a later From<T>(...) configuration for the same type is ignored, so the
first registration wins.
public EntityMetadataBuilder<T> HasQueryFilter(Expression<Func<T, bool>> filter)
Parameters
filterExpression<Func<T, bool>>The filter predicate over the entity.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
HasQueryFilter(string, Func<EntityBuilder<T>, IDataContext, EntityBuilder<T>>?)
Declares a named global query filter in the builder-function form: the key occupies a slot so the
filter can be targeted by the key-based IgnoreFilters overload. A repeated call with the
same key replaces the earlier filter; passing a null
filter removes the slot. The function receives the executing
IDataContext and must express the filter by calling Where; only that predicate
is merged. The mapping is registered once per entity type: a later From<T>(...)
configuration for the same type is ignored, so the first registration wins.
public EntityMetadataBuilder<T> HasQueryFilter(string filterKey, Func<EntityBuilder<T>, IDataContext, EntityBuilder<T>>? filter)
Parameters
filterKeystringThe filter key; null, empty or whitespace declares an anonymous filter.
filterFunc<EntityBuilder<T>, IDataContext, EntityBuilder<T>>The filter function that appends the predicate with
Where, or null to remove the named slot.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
HasQueryFilter(string, Expression<Func<T, IDataContext, bool>>?)
Declares a named global query filter: the key occupies a slot so the filter can be targeted by
the key-based IgnoreFilters overload. A repeated call with the same key replaces
the earlier filter; passing a null filter removes the slot.
The predicate receives the executing IDataContext. The mapping is registered once
per entity type: a later From<T>(...) configuration for the same type is ignored, so
the first registration wins.
public EntityMetadataBuilder<T> HasQueryFilter(string filterKey, Expression<Func<T, IDataContext, bool>>? filter)
Parameters
filterKeystringThe filter key; null, empty or whitespace declares an anonymous filter.
filterExpression<Func<T, IDataContext, bool>>The context-aware filter predicate, or null to remove the named slot.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.
Property(Expression<Func<T, object>>)
Declares a fluent mapping for the property selected by propertySelector.
public EntityPropertyBuilder<T> Property(Expression<Func<T, object>> propertySelector)
Parameters
propertySelectorExpression<Func<T, object>>Selects the property to map; the expression must produce a PropertyInfo.
Returns
- EntityPropertyBuilder<T>
A builder for the selected property's column mapping.
Table(string)
Overrides the table name for the entity instead of deriving it from the CLR type or attributes.
public EntityMetadataBuilder<T> Table(string tableName)
Parameters
tableNamestringThe table name to map the entity to.
Returns
- EntityMetadataBuilder<T>
This builder, for chaining.