Table of Contents

Class OwnershipBuilder<TEntity, TDependentEntity>

Namespace
Microsoft.EntityFrameworkCore.Metadata.Builders
Assembly
Microsoft.EntityFrameworkCore.dll

Provides a simple API for configuring an ownership.

public class OwnershipBuilder<TEntity, TDependentEntity> : OwnershipBuilder, IInfrastructure<IConventionForeignKeyBuilder> where TEntity : class where TDependentEntity : class

Type Parameters

TEntity
TDependentEntity
Inheritance
OwnershipBuilder<TEntity, TDependentEntity>
Implements
Inherited Members
Extension Methods

Remarks

See Modeling entity types and relationships for more information and examples.

Constructors

OwnershipBuilder(IMutableEntityType, IMutableEntityType, IMutableForeignKey)

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

[EntityFrameworkInternal]
public OwnershipBuilder(IMutableEntityType principalEntityType, IMutableEntityType dependentEntityType, IMutableForeignKey foreignKey)

Parameters

principalEntityType IMutableEntityType
dependentEntityType IMutableEntityType
foreignKey IMutableForeignKey

OwnershipBuilder(InternalForeignKeyBuilder, OwnershipBuilder, bool, bool, bool)

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

[EntityFrameworkInternal]
protected OwnershipBuilder(InternalForeignKeyBuilder builder, OwnershipBuilder oldBuilder, bool foreignKeySet = false, bool principalKeySet = false, bool requiredSet = false)

Parameters

builder InternalForeignKeyBuilder
oldBuilder OwnershipBuilder
foreignKeySet bool
principalKeySet bool
requiredSet bool

Methods

HasAnnotation(string, object?)

Adds or updates an annotation on the foreign key. If an annotation with the key specified in annotation already exists its value will be updated.

public virtual OwnershipBuilder<TEntity, TDependentEntity> HasAnnotation(string annotation, object? value)

Parameters

annotation string

The key of the annotation to be added or updated.

value object

The value to be stored in the annotation.

Returns

OwnershipBuilder<TEntity, TDependentEntity>

The same builder instance so that multiple configuration calls can be chained.

HasForeignKey(Expression<Func<TDependentEntity, object?>>)

Configures the property(s) to use as the foreign key for this relationship.

public virtual OwnershipBuilder<TEntity, TDependentEntity> HasForeignKey(Expression<Func<TDependentEntity, object?>> foreignKeyExpression)

Parameters

foreignKeyExpression Expression<Func<TDependentEntity, object>>

A lambda expression representing the foreign key property(s) (t => t.Id1).

If the foreign key is made up of multiple properties then specify an anonymous type including the properties (t => new { t.Id1, t.Id2 }). The order specified should match the order of corresponding properties in HasPrincipalKey(Expression<Func<TEntity, object?>>).

Returns

OwnershipBuilder<TEntity, TDependentEntity>

The same builder instance so that multiple configuration calls can be chained.

Remarks

If the specified property name(s) do not exist on the entity type then a new shadow state property(s) will be added to serve as the foreign key. A shadow state property is one that does not have a corresponding property in the entity class. The current value for the property is stored in the ChangeTracker rather than being stored in instances of the entity class.

If HasPrincipalKey(Expression<Func<TEntity, object?>>) is not specified, then an attempt will be made to match the data type and order of foreign key properties against the primary key of the principal entity type. If they do not match, new shadow state properties that form a unique index will be added to the principal entity type to serve as the reference key.

HasForeignKey(params string[])

Configures the property(s) to use as the foreign key for this relationship.

public virtual OwnershipBuilder<TEntity, TDependentEntity> HasForeignKey(params string[] foreignKeyPropertyNames)

Parameters

foreignKeyPropertyNames string[]

The name(s) of the foreign key property(s).

Returns

OwnershipBuilder<TEntity, TDependentEntity>

The same builder instance so that multiple configuration calls can be chained.

Remarks

If the specified property name(s) do not exist on the entity type then a new shadow state property(s) will be added to serve as the foreign key. A shadow state property is one that does not have a corresponding property in the entity class. The current value for the property is stored in the ChangeTracker rather than being stored in instances of the entity class.

If HasPrincipalKey(params string[]) is not specified, then an attempt will be made to match the data type and order of foreign key properties against the primary key of the principal entity type. If they do not match, new shadow state properties that form a unique index will be added to the principal entity type to serve as the reference key.

HasPrincipalKey(Expression<Func<TEntity, object?>>)

Configures the unique property(s) that this relationship targets. Typically you would only call this method if you want to use a property(s) other than the primary key as the principal property(s). If the specified property(s) is not already a unique constraint (or the primary key) then a new unique constraint will be introduced.

public virtual OwnershipBuilder<TEntity, TDependentEntity> HasPrincipalKey(Expression<Func<TEntity, object?>> keyExpression)

Parameters

keyExpression Expression<Func<TEntity, object>>

A lambda expression representing the reference key property(s) (t => t.Id).

If the principal key is made up of multiple properties then specify an anonymous type including the properties (t => new { t.Id1, t.Id2 }). The order specified should match the order of corresponding properties in HasForeignKey(Expression<Func<TDependentEntity, object?>>).

Returns

OwnershipBuilder<TEntity, TDependentEntity>

The same builder instance so that multiple configuration calls can be chained.

HasPrincipalKey(params string[])

Configures the unique property(s) that this relationship targets. Typically you would only call this method if you want to use a property(s) other than the primary key as the principal property(s). If the specified property(s) is not already a unique constraint (or the primary key) then a new unique constraint will be introduced.

public virtual OwnershipBuilder<TEntity, TDependentEntity> HasPrincipalKey(params string[] keyPropertyNames)

Parameters

keyPropertyNames string[]

The name(s) of the reference key property(s).

Returns

OwnershipBuilder<TEntity, TDependentEntity>

The same builder instance so that multiple configuration calls can be chained.