Base solution for your next web application
Open Closed

extended ABP Entity - not retreiving all data #3866


User avatar
0
rafalpiotrowski created

Hi, I have extended User entity like below:

public class User : AbpUser<User>
    {
        public const int MaxPhoneNumberLength = 24;
.....
        //Can add application specific user properties here
        public MyCompanyName.AbpZeroTemplate.XXX.Address HomeAddress { get; set; }

by adding HomeAddress of class Address

Address class looks like this:

namespace MyCompanyName.AbpZeroTemplate.XXX
{
    public class Address : ValueObject<Address>
    {
        public Country Country { get; set; } //A reference to a Country entity.
        public string City { get; set; }
        public string Street { get; set; }
        public string PostalCode { get; set; }
        public int? Number { get; set; }

        public Address()
        {
        }

        public Address(Country country, string city, string street, int? number, string postalCode)
        {
            Country = country;
            City = city;
            Street = street;
            Number = number;
            PostalCode = postalCode;
        }
    }

    [Table("Countries")]
    public class Country : FullAuditedEntity<int>, IMustHaveTenant
    {
        public int TenantId { get; set; }

        /// <summary>
        /// ISO 3166 Codes
        /// </summary>
        public virtual string CodeA2 { get; set; }
        /// <summary>
        /// ISO 3166 Codes
        /// </summary>
        public virtual string CodeA3 { get; set; }
        public virtual string Name { get; set; }

        public Country() { }
        public Country(string codeA2, string codeA3, string name) { CodeA2 = codeA2; CodeA3 = codeA3; Name = name; }
    }
}

Thanks to the upgrade to EFCore 2.0 we are able to use ValueObjects and thats great! It works, AbpUsers table is extended with HomeAddress_XYZ columns as per class structure above. It has HomeAddress_CountryId as the foreign id. all is good.

Problem comes when we edit an user, call to:

[AbpAuthorize(AppPermissions.Pages_Administration_Users_Create, AppPermissions.Pages_Administration_Users_Edit)]
        public async Task<GetUserForEditOutput> GetUserForEdit(NullableIdDto<long> input)
        {
...
               //Editing an existing user
                var user = await UserManager.GetUserByIdAsync(input.Id.Value);
// !!! HERE: user has HomeAddress set to an instance of Address, but HomeAddress.Country is null !!!
...
}

variable user has HomeAddress set to an instance of Address, but HomeAddress.Country is null !!!

Is it possible to do some kind of an include on Country class or something to let know the UserManager to extract country data?

thanks


2 Answer(s)
  • User Avatar
    0
    strix20 created

    Did you .Include the country?

    EF Core does not support lazy loading.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks @strix20 :), yes you must include Country.