Base solution for your next web application
Open Closed

Error on EnsureLoadedAsync #3670


User avatar
0
kwanp created

Hi i just download new update version 4.2.1 Asp.net core & angular, and try to start coding again follow DEVELOPING AN APPLICATION STEP BY STEP and i don't know why i facing this error when adding phone service

public class PersonAppService : KWANPServiceBase, IPersonAppService
    {
        private readonly IRepository<Person> _personRepository;
        private readonly IRepository<Phone, long> _phoneRepository;

        public PersonAppService(IRepository<Person> personRepository, IRepository<Phone, long> phoneRepository)
        {
            _personRepository = personRepository;
            _phoneRepository = phoneRepository;
        }

        public ListResultDto<PersonListDto> GetPeople(GetPeopleInput input)
        {
            var persons = _personRepository
                .GetAll()
                .Include(p => p.Phones)
                .WhereIf(
                    !input.Filter.IsNullOrEmpty(),
                    p => p.Name.Contains(input.Filter) ||
                            p.Surname.Contains(input.Filter) ||
                            p.EmailAddress.Contains(input.Filter)
                )
                .OrderBy(p => p.Name)
                .ThenBy(p => p.Surname)
                .ToList();

            return new ListResultDto<PersonListDto>(persons.MapTo<List<PersonListDto>>());
        }
        //return new ListResultDto<PersonListDto>(ObjectMapper.Map<List<PersonListDto>>(people));
        public async Task CreatePerson(CreateOrUpdatePersonInput input)//(CreatePersonInput input)
        {
            if (input.Id != null)
            {
                var person = _personRepository.FirstOrDefault(p => p.Id == input.Id);
                ObjectMapper.Map(input, person);
                await _personRepository.InsertOrUpdateAsync(person);
            }
            else
            {
                var person = _personRepository.FirstOrDefault(p => p.EmailAddress == input.EmailAddress);
                if (person != null)
                {
                    throw new UserFriendlyException("There is already a person with given email address");
                }
                else
                {
                    person = ObjectMapper.Map<Person>(input);
                    //await _personRepository.InsertAsync(person); InsertOrUpdate
                    await _personRepository.InsertOrUpdateAsync(person);
                }
            }

        }

        public async Task DeletePerson(EntityDto input)
        {
            await _personRepository.DeleteAsync(input.Id);
        }

        public async Task DeletePhone(EntityDto<long> input)
        {
            await _phoneRepository.DeleteAsync(input.Id);
        }

        public async Task<PhoneInPersonListDto> AddPhone(AddPhoneInput input)
        {
            var person = _personRepository.Get(input.PersonId);
            await _personRepository.EnsureLoadedAsync(person, p => p.Phones);

            var phone = ObjectMapper.Map<Phone>(input);
            person.Phones.Add(phone);

            //Get auto increment Id of the new Phone by saving to database
            await CurrentUnitOfWork.SaveChangesAsync();

            return ObjectMapper.Map<PhoneInPersonListDto>(phone);
        }
    }
[Table("PbPersons")]
    public class Person : FullAuditedEntity
    {
        public const int MaxNameLength = 32;
        public const int MaxSurnameLength = 32;
        public const int MaxEmailAddressLength = 255;

        public virtual ICollection<Phone> Phones { get; set; }

        [Required]
        [MaxLength(MaxNameLength)]
        public virtual string Name { get; set; }

        [Required]
        [MaxLength(MaxSurnameLength)]
        public virtual string Surname { get; set; }

        [MaxLength(MaxEmailAddressLength)]
        public virtual string EmailAddress { get; set; }

        [MaxLength(30)]
        public virtual string Phonetype { get; set; }

        
    }
[Table("PbPhones")]
    public class Phone : CreationAuditedEntity<long>
    {
        public const int MaxNumberLength = 16;

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

        //[Required]
        //public virtual PhoneType Type { get; set; }

        [Required]
        [MaxLength(MaxNumberLength)]
        public virtual string Number { get; set; }

        public virtual decimal SavingAmt { get; set; }
    }

Thank you


3 Answer(s)
  • User Avatar
    0
    mdonogma created

    I'm getting the same error on the MVC JQUERY Project (.net core)

    if I comment out the line then run project. I get attached error message (see screenshot)

    HandlerException: Can't create component 'Acme.PhoneBook.PhoneBook.PersonAppService' as it has dependencies to be satisfied.

  • User Avatar
    0
    mdonogma created

    if you comment out the line then run the .mvc project - do you get the same error as per screenshot i posted earlier.

    be interesting to see if we are seeing the same result.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry for the late reply, somehow we have missed this question. EnsureLoadedAsync is divided into two parts EnsureCollectionLoadedAsync and EnsurePropertyLoadedAsync, so you need to use EnsurePropertyLoadedAsync here.

    Can you try it like that ?

    We will upgrade documentation in a short time.

    Thanks.