C# API在reviews属性中返回null
我正在尝试创建一个简单的方法来检索酒店实体的信息,但是在尝试获取用户添加的评论时遇到了问题。在数据库中,审查实体存储有5个属性:C# API在reviews属性中返回null,c#,asp.net-core,automapper,C#,Asp.net Core,Automapper,我正在尝试创建一个简单的方法来检索酒店实体的信息,但是在尝试获取用户添加的评论时遇到了问题。在数据库中,审查实体存储有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;
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; }
}
我创建了Map方法以供查看:
CreateMap<Review, ReviewForDetailedDto>();
所以我想要的是评论应该通过AutoMapper返回ReviewForDetailDto中声明的所有属性
请提供帮助。您的ReviewReceived酒店名称在您的酒店模型中
不是同一个人的名字
HotelForDetailedTo模型中的Reviews属性
您可以在酒店模型中使用FormMember方法重命名它或配置映射
不是同一个人的名字
HotelForDetailedTo模型中的Reviews属性
您可以重命名它或使用FormMember方法配置映射是否可以显示您的MapperConfiguration?此外,您的问题似乎与asp.net完全无关。您可以考虑用AutoMapper更改标题标题和标签。我只是简单地将AutoPaulService添加到StutupC:Services。你能展示一下你的地图配置吗?此外,您的问题似乎与asp.net完全无关。您可以考虑用AutoMapper更改标题标题和标签。我只是简单地将AutoPaulService添加到StutupC:Services。
[HttpGet("{id}")]
public async Task<IActionResult> GetHotel(int id)
{
var hotel = await Myrepository.GetHotel(id);
var hotelToReturn = IMapper.Map<HotelForDetailedDto>(hotel);
return Ok(hotelToReturn);
}
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; }
}
public class ReviewForDetailedDto
{
public int Id { get; set; }
public int SenderId { get; set; } // userId
public int Point { get; set; }
public string Comment { get; set; }
}
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
}