2

In my EF data model I have a Location entity and LocationType entity. Location has FkLocationTypeID property. Now if I return a Location object from my API endpoint, it returns a large object which all navigation properties attached to it. Therefore I created a LocationDto to return only those properties I want. My DTO looks something like this:

 public class LocationDto
{
    public int LocationId { get; set; }
    public LocationDto ParentLocation { get; set; }
    public LocationType LocationType { get; set; }
    public string LocationName { get; set; }
    public string LocationCode { get; set; }
    // other properties here
}

Now the problem is - I have a LocationType property in my DTO, which is an EF object. The moment I add it, my JSON is again very lengthy, because of all the associations with the LocationType.

Is there'a way to avoid this and only retrieve a LocationType object?

Following is how my json looks like after including the LocationType.

    {
  "locationId": 0,
  "locationType": {
    "locationTypeId": 0,
    "locationTypeName": "string",
    "locationTypeDisplayName": "string",
    "localizedKey": "string",
    "locations": [
      {
        "locationId": 0,
        "fkParentLocationId": 0,
        "fkLocationTypeId": 0,
        "fkTimeZoneId": 0,
        "locationName": "string",
        "locationDisplayName": "string",
        "locationCode": "string",
        "address1": "string",
        "address2": "string",
        "city": "string",
        "state": "string",
        "country": "string",
        "zipCode": "string",
        "phoneNumber": "string",
        "faxNumber": "string",
        "phoneExtention": "string",
        "email": "string",
        "longitude": 0,
        "latitude": 0,
        "useAppointments": true,
        "availabilityWindowDays": 0,
        "appointmentCutOffDays": 0,
        "dailySummaryEmailTime": "string",
        "durationBeforeFirstApptHours": 0,
        "reminderBeforeApptSmsHours": 0,
        "reminderBeforeApptEmailHours": 0,
        "createdBy": 0,
        "createdDate": "2018-10-26T06:51:00.288Z",
        "changedBy": 0,
        "changedDate": "2018-10-26T06:51:00.288Z",
        "activeStatus": 0,
        "enableStaffSelection": true,
        "showInWidget": true,
        "autoAssign_FloatPriorityMode": 0,
        "googleReserveEnabled": true,
        "messageLogs": [
          {
            "messageLogId": 0,
            "fkMessageFormatTypeId": 0,
            "fkMessageTypeId": 0,
            "fkLocationId": 0,
            "fkUserId": 0,
            "fkAppointmentId": 0,
            "sentTime": "2018-10-26T06:51:00.288Z",
            "messageUid": "string",
            "messageFormatType": {
              "messageFormatTypeId": 0,
              "messageFormatTypeName": "string",
              "messageTemplates": [
                {
                  "messageTemplateId": 0,
                  "fkMessageFormatTypeId": 0,
            .....................................
          }
       }
Shcherban
  • 192
  • 2
  • 10
devC
  • 1,266
  • 4
  • 22
  • 48
  • 1
    Its better to not have EF entity inside DTO https://stackoverflow.com/questions/34801414/preventing-automatic-population-of-circular-navigation-properties-in-entity-fram – Shcherban Oct 26 '18 at 06:59
  • Right, my option is to create a DTO for LocationType as well. I wanted to see whether there's an alternative where I could simply avoid all circular references without doing that. – devC Oct 26 '18 at 07:01
  • also you can disable proxy creation https://stackoverflow.com/a/32012202/1644522 – Shcherban Oct 26 '18 at 07:03

1 Answers1

1
  • do not mix the EF models and the DTOs!
  • create a complete set of DTOs in a separate namespace (project, assembly)
  • use a mapper (ie Automap or similar) to map between the EF models and the DTOs
Z. Danev
  • 11,848
  • 1
  • 29
  • 48