WithOptionalDependent vs WithOptionalPrinciple - Definitive Answer?

For example, lets modify your EntityB by navigation property and make BId nullable (as we are talking about optional relationship).

class MyEntityA
{
    [Key]
    public int Id { get; set; }
    public int? BId { get; set; }

    [ForeignKey("BId")]
    public virtual MyEntityB B { get; set; }
}

class MyEntityB
{
    [Key]
    public int Id { get; set; }

    public virtual MyEntityA A { get; set; }
}

then we can use:

modelBuilder.Entity<MyEntityB>().HasOptional(a => a.A).WithOptionalPrincipal();

MyEntityA has FK to MyEntityB, so in your example you configure MyEntityA and use WithOptionalDependent. But you can start configuration from MyEntityB-side, then you need WithOptionalPrincipal.


The answer to your question is: "The entity type being configured" is MyEntityA

This can be seen definitively by looking at the documentation for

OptionalNavigationPropertyConfiguration<TEntityType, TTargetEntityType>

which is the type returned by HasOptional and which says:

TTargetEntityType

The entity type that the relationship targets.

which provides more context for the phrases:

The entity type being configured

The entity type that the relationship targets

So, in your case you get back from HasOptional an

OptionalNavigationPropertyConfiguration<MyEntityA, MyEntityB>

Thus, WithOptionalDependent means that MyEntityB will be the Principal with an optional Navigation Property pointing back to MyEntityA (specified via the overload's lambda parameter) and MyEntityA will be the Dependent and contain a Foreign Key and Navigation Property (as specified in the lambda parameter of HasOptional). This is the scenario in your model.

Conversely, WithOptionalPrincipal means that MyEntityA will be the Principal and MyEntityB the Dependent with Foreign Key and Navigation Property.