Home > Blockchain >  EF7 - Properties object from entity class are not being filled
EF7 - Properties object from entity class are not being filled

Time:12-13

Good night!

I am playing with EF7 to learn about it.

I've created the next classes:

public class Lawsuit
    {
        /// <summary>
        /// Lawsuit identificator.
        /// </summary>
        public Guid LawsuitId { get; set; }

        /// <summary>
        /// Date time asigned where lawsuit was created.
        /// </summary>
        public DateTime DateTimeCreated { get; set; }

        /// <summary>
        /// First part envolved signature object.
        /// </summary>
        public virtual Signature? FirstPartyEnvolved { get; set; }

        /// <summary>
        /// First part envolved signature object.
        /// </summary>
        public virtual Signature? SecondPartyEnvolved { get; set; }

        /// <summary>
        /// Winner position indicator
        /// </summary>
        public WinnerPosition? WinnerPosition { get; set; }
    }
public class Signature
    {
        /// <summary>
        /// Id from signature.
        /// </summary>
        public Guid Id { get; set; }

        /// <summary>
        /// String which saves text received from contract signatures.
        /// </summary>
        public string? Signatures { get; set; }

    }

At insert, the data is created correctly at database (SQL Server), but when I try to obtain the data, FirstPartyEnvolved and SecondPartyEnvolved attributes are empty.

Example of response:

[
  {
    "lawsuitId": "6ece06da-d5f9-40e7-a7a3-2816618e7869",
    "dateTimeCreated": "2022-12-12T22:05:24.4717517",
    "firstPartyEnvolved": null,
    "secondPartyEnvolved": null,
    "winnerPosition": 1
  }
]

I was checking if the DTO object was not parsing well the data but the problem seems to be at the Entity object

public Task Handle()
        {
            var Lawsuites_repo = LawsuitRepository.GetAllLawsuit();
            var Lawsuites = Lawsuites_repo.Select(x =>
                new LawsuitDTO
                {
                    LawsuitId = x.LawsuitId,
                    DateTimeCreated = x.DateTimeCreated,
                    WinnerPosition = x.WinnerPosition ?? 0,
                    FirstPartyEnvolved = x.FirstPartyEnvolved,
                    SecondPartyEnvolved = x.SecondPartyEnvolved
                });

            GetAllLawsuitsOutputPort.Handle(Lawsuites);
            return Task.CompletedTask;

        }

I've tried to changed the Signatures objects for GUID and I am receiving well this data. Seems a problem parsing the object from the EF context to the Entity class.

CodePudding user response:

You haven't detailed what is in your GetAllLawsuit() method, but EF does not eager load related entities by default, so you are likely just ToList()'ing the content, then the collection will be loaded from the database with null references for the Signatures

If you remove the call to GetAllLawsuit() and use the Select projection directly from the DbContext, then it will work.

You can also force EF to eager load your related entities by using the Include extension method in your GetAllLawsuit method.

  • Related