Base solution for your next web application
Open Closed

Working with related Entities #3090


User avatar
0
paulc66 created

Running into an issue - or more so, I'm a little stumped.

I have two entities setup:

[Table("Patient")]
    public class Patient : FullAuditedEntity
    {
        //Schema Restrictions
        public const int MaxFirstNameLength = 50;
        public const int MaxLastNameLength = 75;
        public const int MaxInsuranceLength = 200;

        //Properties
        [Required]
        [MaxLength(MaxFirstNameLength)]
        public string FirstName { get; set; }

        [Required]
        [MaxLength(MaxLastNameLength)]
        public string LastName { get; set; }

        [Required]
        public DateTime DateOfBirth { get; set; }

        [Required]
        public Int32 StatusId { get; set; }

        [Required]
        public Int32 PrefrerredCommunicationId { get; set; }

        [Required]
        public Int32 PreferredLanguageId { get; set; }

        [Required]
        [MaxLength(MaxInsuranceLength)]
        public string PrimaryInsurance { get; set; }

        //Phone

        //Address
        [Required]
        public virtual PatientAddress Addresses { get; set; }

        [Required]
        public bool IsActive { get; set; }

        //Setup the Active interface - this automagically setsup our Entity to have the IsActive Bit.
        protected interface IPassivable
        {
            bool IsActive { get; set; }
        }

        //Constructor, set's active and createtime by default.
        protected Patient()
        {
            IsActive = true;
            CreationTime = Clock.Now;
        }

    }
[Table("PatientAddress")]
    public class PatientAddress : FullAuditedEntity
    {

        //Schema Restrictions
        public const int MaxAddressLength = 100;
        public const int MaxCityLength = 50;
        public const int MaxStateLength = 50;
        public const int MinZipLength = 5;
        public const int MaxZipLength = 10;

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

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

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

        [Required]
        [MaxLength(MaxStateLength)]
        public string State { get; set; }

        [Required]
        [MaxLength(MaxZipLength)]
        [MinLength(MinZipLength)]
        public string Zip { get; set; }

        [Required]
        public bool IsActive { get; set; }

        [Required]
        [ForeignKey(nameof(PatientId))]
        public int PatientId { get; set; }

        //Setup the Active interface - this automagically setsup our Entity to have the IsActive Bit.
        protected interface IPassivable
        {
            bool IsActive { get; set; }
        }

        public PatientAddress(int patientId)
        {
            PatientId = patientId;
        }

    }

What I was trying to do was create a Patient, and a PatientAddress entity -> from there I wanted to create the relationship from the Address -> Patient; and include a virtual Address object on the patient - my thought being that then I could just add Address to a patient object when creating/editing, and then just include the address via relationship when getting a patient.

My issue is that within my Patient Application Service, I have no way of including the Address Entity that I've put on the Patient when trying to pull a single patient record out. Should I be creating my own custom Repository at this point or?

public async Task<Patient> GetPatientById(GetPatientDto input)
        {
            return await _patientRepostiory.GetAsync(input.Id);
        }

I have no option to use .Include, or Query from there.

This is what I'm ultimately trying to get to as an output:

{
  "firstName": "string",
  "lastName": "string",
  "dateOfBirth": "2017-05-02T19:49:06.147Z",
  "statusId": 0,
  "prefrerredCommunicationId": 0,
  "preferredLanguageId": 0,
  "primaryInsurance": "string",
  "address": {
    "address1": "string",
    "address2": "string",
    "city": "string",
    "state": "string",
    "zip": "string",
    "patientId": 0
  },
  "isActive": true,
  "id": 0
}

I'm I going about this the completely wrong way or is there a better way to accomplish this?

Thanks.


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

    Hi,

    You can use EnsureLoadedAsync to include related entity if you are using one of the latest versions of ABP. Otherwise you need to use GetAll & Include and then use First linq methods.