Dotnet 5.0 referencing child entity without include

Viewed 29

I'm on a dotnet project using dotnet 5. This project are not code first, I'm creating the entity relationship with "dotnet ef dbcontext scaffold" and I'm having this issue:

I have an entity that points to itself: LegalPerson -> LegalPersonParent

    public partial class LegalPerson
    {
        [Key]
        [Column("id")]
        public long Id { get; set; }

        [Column("idPessoa")]
        public long PersonId { get; set; }

        [Column("idPessoaJuridicaPai")]
        public long? LegalPersonParentId { get; set; }

        [Column("idRamoDeAtividade")]
        public long? ActivityBranchId { get; set; }

        [ForeignKey(nameof(LegalPersonParentId))]
        public virtual LegalPerson LegalPersonParent { get; set; }

        [ForeignKey(nameof(PersonId))]
        public virtual Person Person { get; set; }

        [ForeignKey(nameof(ActivityBranchId))]
        public virtual ActivityBranch ActivityBranch { get; set; }

    }

But when I get all LegalPersons, I'm getting LegalPersonParent too.

context.LegalPersons.ToList();

returns:

[
    {
        "id": 1365,
        "personId": 1,
        "legalPersonParentId": 1367,
        "activityBranchId": 1,
        "legalPersonParent": {
            "id": 1367,
            "personId": 6,
            "legalPersonParentId": null,
            "activityBranchId": null,
            "legalPersonParent": null,
            "person": null,
            "activityBranch": null
        },
        "person": null,
        "activityBranch": null
    },
    ... more data
]

What I want is to only add LegalPersonParent when I add Include method like:

context.LegalPersons.Include(legalPerson => legalPerson.LegalPersonParent).ToList();

There are more interesting things happening. On the return of context.LegalPersons.ToList(); previously shon, the ActivityBranch and Person are working correctly not getting the entity, only the reference id -> activityBranchId and personId

The other interesting thing happening is when i search for a single LegalPerson, it's not getting the LegalPersonParent:

_context.LegalPersons.FirstOrDefault(legalPerson => legalPerson.Id == id);

returns:

{
    "id": 1365,
    "personId": 1,
    "legalPersonParentId": 1367,
    "activityBranchId": 1,
    "legalPersonParent": null,
    "person": null,
    "activityBranch": null
}

The DbContext:

modelBuilder.Entity<LegalPerson>(entity =>
{
     entity.HasIndex(e => e.PersonId, "IDX_PessoaJuridica_idPessoa")
         .HasFillFactor((byte)70);
});
1 Answers

Context cache the result, when you get all of the LegalPerson entities, EF know what is the LegalPerson of LegalPersonParent ID, so put it there and when you get only one LegalPerson, EF doesn't know what is the LegalPerson of LegalPersonParent ID if the LegalPersonParentID different from the LegalPerson selected

If you get all ActivityBranch before get all LegalPerson with a same context, you see all of the ActivityBranch resolved too

Related