Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Related entity not loading #2815


User avatar
0
exlnt created

I have this method, below, for getting one single entity:

public async Task<EditNurseHomeDto> GetHomeForEdit(NullableIdDto input)
        {
            var nurseHomeEdit = (await _homeRepository.GetAsync((int)input.Id));
            var nh = nurseHomeEdit.MapTo<EditNurseHomeDto>();
            return nh;
        }

I have used this exact same method for another entity. In that other entity when the above method executes, it brings the data for the related entity (notes). However in the above method it is not bringing the related entity (Notes) back. The DTOs are structured virtually the same for both entities, yet it does not work in the above method?


11 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share EditNurseHomeDto class ?

    Thanks.

  • User Avatar
    0
    exlnt created

    Please ignore this post.

    My home record had a null value in the NoteID column and that is why note entity was not loading. :shock:

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks,

    It will be hard to find this for us :).

  • User Avatar
    0
    exlnt created

    I am moving my MVC5 template app to .Net Core 1.1 MVC template. So I am porting over all my working code from the prior solution to the new .Net core solution. I have converted just 2 entities so far into the new solution. The first one was simple as it had no related data. My second entity has several relationships. The relationship to "Note" entity is not working.

    public async Task<EditCompanyDto> GetCompanyForEdit(NullableIdDto input)
            {
                var companyEditDto = (await _companyRepository.GetAsync((int)input.Id));
                var cmp = companyEditDto.MapTo<EditCompanyDto>();
                return cmp;
            }
    

    The code above works just fine in my existing MVC5 template solution. When I run this in the .Net core solution, it does not bring the "Note" entity data. This time my company record has a valid NoteId in the DB.

    Here is my EditCompanyDto

    [AutoMap(typeof(Company))]
        public class EditCompanyDto : EditCompanyDtoForUpdate
        {
            public DateTime CreationTime { get; set; }
    
            public int CreatorUserId { get; set; }
    
            public string CreatedByUser { get; set; }
    
            public DateTime LastModificationTime { get; set; }
    
            public int LastModifierUserId { get; set; }
    
            public string LastModifiedByUser { get; set; }
    
            public EditNoteHeaderDto Note { get; set; }
    
            public List<EditCompanyAddressDto> Addresses { get; set; }
    
            public List<EditCompanyContactDto> Contacts { get; set; }
    
            public EditCompanyDto()
            {
                Addresses = new List<EditCompanyAddressDto>();
                Contacts = new List<EditCompanyContactDto>();
            }
        }
    

    Can you please tell me if I need to do something different in .Net core to make this work?

  • User Avatar
    0
    alper created
    Support Team

    hello

    you need to include Note entity. so your code must be something like this

    _homeRepository.GetAll().Include(x=>x.Note).Where(x=>x.Id==input.Id)

  • User Avatar
    0
    exlnt created

    I tried that out and it leads to multiple other issues. There is no ASYNC IEnumerable version of GetAll method. So that forces me to change the method signature and the signature of the MVC controller method that calls this app service method. Plus it fails in the app service method on this line:

    var cmp = companyEditDto.MapTo<EditCompanyDto>();
    

    With the error below:

    Missing type map configuration or unsupported mapping.
    Mapping types:
    EntityQueryable`1 -> EditCompanyDto
    Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[[EXLNT.NursingOps17.NursingOps.Company, EXLNT.NursingOps17.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null]] -> EXLNT.NursingOps17.NursingOps.Dto.EditCompanyDto
    

    However, what I dont understand is I have two other related entities address and contact on this entity and they are both getting returned with the code I shared. Note entity is the only one it keeps returning as NULL. Is this something related to EF core change?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @exlnt,

    Can you share full code of your app service ?

    Thanks.

  • User Avatar
    0
    exlnt created

    Here is my app service code: <a class="postlink" href="https://docs.google.com/document/d/1KrL1oCvwssdyDGfcue8HlANAUtI1bCuH2cToMfiVSxM/edit?usp=sharing">https://docs.google.com/document/d/1KrL ... sp=sharing</a>

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @exlnt,

    Entity Framework Core does not support lazy loading at the moment. Can you try to load your Note entity using _companyRepository.EnsureLoadedAsync after below line:

    var companyEditDto = (await _companyRepository.GetAsync((int)input.Id));
    

    Thanks.

  • User Avatar
    0
    exlnt created

    I posted this same issue question on stackoverflow and a person suggested the below code and it worked!

    var companyEditDto = _companyRepository.GetAll()
                                   .Include(c => c.Addresses)
                                   .Include(c => c.Contacts)
                                   .Include(c => c.Note).ThenInclude(N => N.Notes)
                                   .FirstOrDefault(x => x.Id == input.Id);
    

    I am concerned about doing the GetAll() in all my GetForEdit app services and how it will impact the performance of my app as more data gets into the DB?

    I tried your suggestion, but I could not figure out or understand what parameters I need to pass to this method. Can you share a full example call and what all parameters I need to pass into this method?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @exlnt,

    I think I understand your case wrong, so don't apply my suggestion. This usage is better for your case.

    Using GetAll returns an IQueryable, so this linq query on returns a single record from database. This will not cause a performance problem I think.

    Thanks.