Base solution for your next web application
Open Closed

Parent related property not loaded #4582


User avatar
0
panic created

Hello, I have a model PropertyBlock which has a parent called PropertyMaster, and also has child models called PropertyUnits. When I call

var propertyBlock = await _propertyBlockRepository.GetAsync(input.Id);

then the parent PropertyMaster is not loaded and also the children collection of PropertyUnits is empty. How can I ask to explicitly load these? Can you please give me some examples of the repository extension methods: EnsureCollectionLoaded<TEntity, TPrimaryKey, TProperty> EnsureCollectionLoadedAsync<TEntity, TPrimaryKey, TProperty> EnsurePropertyLoaded<TEntity, TPrimaryKey, TProperty> EnsurePropertyLoadedAsync<TEntity, TPrimaryKey, TProperty>

Many thanks!

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Abp.Domain.Entities.Auditing;
using System.Collections.Generic;

namespace MyPropMan.Entities
{
	[Table("PropertyBlock")]
    public partial class PropertyBlock : FullAuditedEntity
    {
		public const int MaxShortidLength     = 55;
		public const int MaxPropnameLength    = 155;
		public const int MaxTypeofLength   	  = 65;
        public const int MaxAddressLength     = 655;
		public const int MaxPostalAreaLength  = 115;
		public const int MaxPostalCodeLength  = 25;
		public const int MaxCityCountryLength = 95;
        public const int MaxTextLength 	 	  = 555;

        public PropertyBlock()
        {
            Banking = new HashSet<Banking>();
            Bill = new HashSet<Bill>();
            Fee = new HashSet<Fee>();
            Journal = new HashSet<Journal>();
            Payment = new HashSet<Payment>();
            PropertyAccount = new HashSet<PropertyAccount>();
            PropertyExpenditure = new HashSet<PropertyExpenditure>();
            PropertyBudget = new HashSet<PropertyBudget>();
            PropertyExpense = new HashSet<PropertyExpense>();
            PropertyUnit = new HashSet<PropertyUnit>();
            PropertyVendor = new HashSet<PropertyVendor>();
            Receipt = new HashSet<Receipt>();
            Statement = new HashSet<Statement>();
        }

        public int Propertymasterid { get; set; }

		[MaxLength(MaxShortidLength)]
        public string Shortid { get; set; }

		[MaxLength(MaxPropnameLength)]
        public string Name { get; set; }

		[MaxLength(MaxAddressLength)]
        public string Address { get; set; }

		[MaxLength(MaxPostalAreaLength)]
        public string Postalarea { get; set; }

		[MaxLength(MaxPostalCodeLength)]
        public string Postalcode { get; set; }

		[MaxLength(MaxCityCountryLength)]
        public string City { get; set; }

		[MaxLength(MaxCityCountryLength)]
        public string Country { get; set; }

		[MaxLength(MaxTypeofLength)]
        public string Typeof { get; set; }

        public short Blockunits { get; set; }

        public bool? Isowncostcentre { get; set; }

        public bool? Isactive { get; set; }

		[MaxLength(MaxTextLength)]
        public string Textnote { get; set; }

        public PropertyMaster Propertymaster { get; set; }
        public ICollection<Banking> Banking { get; set; }
        public ICollection<Bill> Bill { get; set; }
        public ICollection<Fee> Fee { get; set; }
        public ICollection<Journal> Journal { get; set; }
        public ICollection<Payment> Payment { get; set; }
        public ICollection<PropertyAccount> PropertyAccount { get; set; }
        public ICollection<PropertyExpenditure> PropertyExpenditure { get; set; }
        public ICollection<PropertyBudget> PropertyBudget { get; set; }
        public ICollection<PropertyExpense> PropertyExpense { get; set; }
        public ICollection<PropertyUnit> PropertyUnit { get; set; }
        public ICollection<PropertyVendor> PropertyVendor { get; set; }
        public ICollection<Receipt> Receipt { get; set; }
        public ICollection<Statement> Statement { get; set; }
    }
}


2 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    You can do it like this:

    var propertyBlock = await _propertyBlockRepository.GetAsync(input.Id);
    
    await _propertyBlockRepository.EnsurePropertyLoadedAsync(propertyBlock, p => p.Propertymaster);
    

    This may make an additional call to the database.

    Alternatively:

    var propertyBlock = await _propertyBlockRepository
        .GetAllIncluding(p => p.Propertymaster)
        .FirstOrDefaultAsync(p => p.Id == input.Id);
    
  • User Avatar
    0
    panic created

    Thank you very much Aaron. Have a good day!