Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EF避免将导航属性附加到DTO_C#_Entity Framework_Dto_Navigation Properties - Fatal编程技术网

C# EF避免将导航属性附加到DTO

C# EF避免将导航属性附加到DTO,c#,entity-framework,dto,navigation-properties,C#,Entity Framework,Dto,Navigation Properties,在我的EF数据模型中,我有一个Location实体和LocationType实体。位置具有FkLocationTypeID属性。现在,如果我从API端点返回一个Location对象,它将返回一个包含所有导航属性的大型对象。因此,我创建了一个LocationDto来只返回我想要的属性。我的DTO看起来像这样: public class LocationDto { public int LocationId { get; set; } public LocationDto Pare

在我的EF数据模型中,我有一个Location实体和LocationType实体。位置具有FkLocationTypeID属性。现在,如果我从API端点返回一个Location对象,它将返回一个包含所有导航属性的大型对象。因此,我创建了一个LocationDto来只返回我想要的属性。我的DTO看起来像这样:

 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
}
现在的问题是-我的DTO中有一个LocationType属性,它是一个EF对象。在我添加它的那一刻,我的JSON又变得非常冗长,因为所有与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,
            .....................................
          }
       }
有没有办法避免这种情况,只检索LocationType对象

下面是我的json在包含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,
            .....................................
          }
       }
  • 不要将EF型号和DTO混用
  • 在单独的命名空间(项目、程序集)中创建一组完整的DTO
  • 使用映射器(即Automap或类似工具)在EF模型和DTO之间映射

最好不要在DTO中包含EF实体,我的选择是为LocationType创建DTO。我想看看是否有一个替代方案,我可以简单地避免所有循环引用而不这样做。你也可以禁用代理创建是的,我认为这是最好的做法。