ApplyOneToOne
Configures a one-to-one relationship where TEntity is the principal entity and TDependent is the dependent entity. The method starts from the principal entity, uses the provided reference navigation, configures the dependent foreign key, applies delete behavior, and optionally sets a principal key.
Use this method when the principal entity has a single dependent reference and the dependent entity owns the foreign key. For relationship shapes with custom inverse navigations or additional constraints, use EF Core's fluent API directly.
Usage
using Microsoft.EntityFrameworkCore;
using AlmightyShogun.EntityFrameworkCore.Utils;
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyOneToOne<User, UserProfile>(
user => user.Profile,
profile => profile.UserId,
isRequired: true,
deleteBehavior: DeleteBehavior.Cascade
);
}Parameters
navigation: Expression<Func<TEntity, TDependent?>>
Reference navigation on the principal entity pointing to the dependent entity.
foreignKey: Expression<Func<TDependent, object?>>
Foreign key property on the dependent entity.
principalKey: Expression<Func<TEntity, object?>>?
Optional principal key property. When omitted, EF Core uses the principal primary key.
Default: null
isRequired: bool
Whether the dependent relationship is required.
Default: true
deleteBehavior: DeleteBehavior
Delete behavior applied to the relationship.
Default: DeleteBehavior.ClientSetNull
Type signature
public void ApplyOneToOne<TEntity, TDependent>(
Expression<Func<TEntity, TDependent?>> navigation,
Expression<Func<TDependent, object?>> foreignKey,
Expression<Func<TEntity, object?>>? principalKey = null,
bool isRequired = true,
DeleteBehavior deleteBehavior = DeleteBehavior.ClientSetNull
) where TEntity : class where TDependent : class;