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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)