Entity Framework Core zero-or-one to zero-or-one relation

c# ef-fluent-api entity-framework-core

Question

Considering these classes:

public class A
{
    public int Id {get; set;}
    public int? BId {get; set;}
    public B B {get; set;}
}

public class B
{
    public int Id {get; set;}
    public int? AId {get; set;}
    public A A {get; set;} 
}

next, using Fluent API

 modelBuilder.Entity<A>()
                .HasOne(a => a.B)
                .WithOne(b => b.A)
                .HasForeignKey<A>(a => a.BId);

The following is how the relevant tables appear after generating the objects and adding them to the database:

  • The [A].BId is set
  • [B].AId is empty

When I use EF Core to fetch data:

  • A.B and A.BId are both set.
  • Zzz-42-zzz, but B.A is set.

What should I do to ensure that B.AId is also set?

1
3
2/3/2019 1:28:28 AM

Accepted Answer

ZZZ_tmp
4
2/5/2019 12:23:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow