c# - API 在评论属性中返回 null
问题描述
我正在尝试创建一个简单的方法 GET 来检索酒店实体的信息,但在尝试获取评论(由用户添加)时卡住了。在数据库中,Review 实体存储有 5 个属性:
int Id(Primarykey)
int SenderId(User who sent review)
int RecipientId(The hotel reviewed by user)
int point
string comment
评论模型:
public class Review
{
public int Id { get; set; }
public User Sender { get; set; }
public int SenderId {get; set; }
public Hotel Recipient { get; set; }
public int RecipientId { get; set; }
public DateTime DateAdded { get; set; }
public int Point { get; set; }
public string Comment { get; set; }
public bool SenderDeleted { get; set; }
}
酒店的汽车旅馆:
public class Hotel
{
public int Id { get; set; }
public string HotelName { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string Address { get; set; }
public int Rooms { get; set; }
public int Beds { get; set; }
public int Bathrooms { get; set; }
public string City { get; set; }
public string Country { get; set; }
public bool Status { get; set; }
public ICollection<Review> ReviewsReceived { get; set; }
}
在 Myrepository.cs 中,我声明了 GetHotel 函数:
public async Task<Hotel> GetHotel(int id)
{
var hotel = await _context.Hotels.Include(r =>
r.ReviewsReceived).FirstOrDefaultAsync(h => h.Id == id);
return hotel;
}
在 HotelsController.cs 中:
[HttpGet("{id}")]
public async Task<IActionResult> GetHotel(int id)
{
var hotel = await Myrepository.GetHotel(id);
var hotelToReturn = IMapper.Map<HotelForDetailedDto>(hotel);
return Ok(hotelToReturn);
}
在 HotelForDetailedDto.cs 中:
public class HotelForDetailedDto
{
public string HotelName { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public string Address { get; set; }
public int Rooms { get; set; }
public int Beds { get; set; }
public int Bathrooms { get; set; }
public string City { get; set; }
public string Country { get; set; }
public bool Status { get; set; }
public ICollection<ReviewForDetailedDto> Reviews { get; set; }
}
在 ReviewForDetailDto.cs 中:
public class ReviewForDetailedDto
{
public int Id { get; set; }
public int SenderId { get; set; } // userId
public int Point { get; set; }
public string Comment { get; set; }
}
我创建了 Map 方法以供审查:
CreateMap<Review, ReviewForDetailedDto>();
这就是我收到的:
{
"hotelName": "Dentrex",
"description": "Eu incididunt pariatur non veniam veniam reprehenderit
cillum adipisicing consequat cillum aliqua in. Ut ut reprehenderit commodo
tempor cillum in sit anim labore in et proident commodo. Fugiat eni.\r\n",
"price": 2349.79,
"address": "Gelston Avenue",
"rooms": 487,
"beds": 2,
"bathrooms": 2,
"city": "Ronco",
"country": "Algeria",
"status": false,
"reviews": null
}
所以我想要的是评论应该通过 AutoMapper 返回在 ReviewForDetailDto 中声明的所有属性。
请帮忙。
解决方案
您的酒店模型中的“ ReviewsReceived ”属性名称与您的HotelForDetailedDto模型中的“ Reviews ”属性名称不同
您可以重命名它或使用 forMember() 方法配置映射
推荐阅读
- mongodb - 在 Mongodb 中获取单个文档的大小?
- reactjs - 传递多个参数后调用两次 useEffect
- python - 使用 Python 将标题添加到 CSV 文件
- swift - 带双引号的 Alamofire 标头
- node.js - 如何检查我的机器人是否可以在 discord.js v12 中添加角色?
- reactjs - Componentdidmount:获取请求并设置状态
- javascript - 我应该在 react-native 项目中将firebase初始化放在哪里?
- unix - 在特殊字段 AWK 中搜索字符
- excel - 将工作表拆分为多个工作表并保持格式化 VBA
- java - Jenkins插件安装问题