简体   繁体   中英

EF7 - Properties object from entity class are not being filled

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM