EF 避免将导航属性附加到 DTO
EF avoid attaching navigation properties to the DTO
在我的 EF 数据模型中,我有一个 Location 实体和 LocationType 实体。位置具有 FkLocationTypeID 属性。现在,如果我 return 来自 API 端点的 Location 对象,它 return 是一个大对象,所有导航属性都附加到它。因此,我创建了一个 LocationDto 到 return 只有那些我想要的属性。我的 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 的关联。
有没有办法避免这种情况,只检索 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 之间进行映射
在我的 EF 数据模型中,我有一个 Location 实体和 LocationType 实体。位置具有 FkLocationTypeID 属性。现在,如果我 return 来自 API 端点的 Location 对象,它 return 是一个大对象,所有导航属性都附加到它。因此,我创建了一个 LocationDto 到 return 只有那些我想要的属性。我的 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 的关联。
有没有办法避免这种情况,只检索 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 之间进行映射