Asp.net web api 如何正确显示用户';s最喜欢的食谱ASP.NET核心MVC

Asp.net web api 如何正确显示用户';s最喜欢的食谱ASP.NET核心MVC,asp.net-web-api,asp.net-core-mvc,Asp.net Web Api,Asp.net Core Mvc,使用.NET Core 3.1.1 MVC+Angular 我在显示用户最喜欢的食谱时遇到了奇怪的问题 当我在响应中调用getFavRecipes时,我在用户(id 1)中有两个fav配方,这与数据库中的相同 然后我调用getUser(id 1),favRecipes为null。。 我的代码: public class User { public int Id { get; set; } public string Username { get;

使用.NET Core 3.1.1 MVC+Angular

我在显示用户最喜欢的食谱时遇到了奇怪的问题

当我在响应中调用getFavRecipes时,我在用户(id 1)中有两个fav配方,这与数据库中的相同

然后我调用getUser(id 1),favRecipes为null。。

我的代码:

  public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public ICollection<UserPhoto> UserPhotos {get; set;}
        public ICollection<Recipe> Recipes {get; set;}
        public ICollection<FavouriteRecipe> FavRecipes {get; set;}

    }



   public class FavouriteRecipe
    {
        public int RecipeId { get; set; }
        public Recipe Recipe { get; set; }
        public int UserId { get; set; }
        public User User { get; set; }
        public DateTime DateLiked { get; set; }

    }


public class Recipe
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Ingredients { get; set; }
    public int PreparationTime { get; set; }
    public string Description { get; set; }
    public DateTime DateAdded { get; set; }
    public ICollection<RecipePhoto> RecipePhotos {get; set;}
    public User User { get; set; }
    public int UserId { get; set; }
}
公共类用户
{
公共int Id{get;set;}
公共字符串用户名{get;set;}
公共ICollection用户照片{get;set;}
公共ICollection配方{get;set;}
公共ICollection FavRecipes{get;set;}
}
公共类偏好配方
{
public int RecipeId{get;set;}
公共配方{get;set;}
public int UserId{get;set;}
公共用户{get;set;}
公共日期时间日期{get;set;}
}
公共课食谱
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串{get;set;}
public int PreparationTime{get;set;}
公共字符串说明{get;set;}
public DateTime DateAdded{get;set;}
公共ICollection RecipePhotos{get;set;}
公共用户{get;set;}
public int UserId{get;set;}
}
用户存储库

  public async Task<User> GetUser(int id)
    {
         var user = await _context.Users
         .Include(p => p.UserPhotos)
         .Include(f=>f.FavRecipes)
         .Include(r => r.Recipes)
            .ThenInclude(r => r.RecipePhotos)
         .FirstOrDefaultAsync(r => r.Id == id);
        return user;
    }


    public Task<List<Recipe>> GetUserFavRecipes(int userId)
    {

        var userFavRecipes = _context.FavouriteRecipes
        .Include(x => x.Recipe)
        .Where(x => x.UserId == userId)
        .Select(x => x.Recipe)?.ToListAsync();

        return userFavRecipes;
    }
public异步任务GetUser(int-id)
{
var user=await\u context.Users
.Include(p=>p.UserPhotos)
.包括(f=>f.FavRecipes)
.包括(r=>r.食谱)
.然后包括(r=>r.RecipePhotos)
.FirstOrDefaultAsync(r=>r.Id==Id);
返回用户;
}
公共任务GetUserFavRecipes(int userId)
{
var userFavRecipes=\u context.favoriteRecipes
.包括(x=>x.Recipe)
.Where(x=>x.UserId==UserId)
.Select(x=>x.Recipe)?.ToListAsync();
返回userFavRecipes;
}
控制器

 [HttpGet("{id}", Name = "GetUser")] 
    public async Task<IActionResult> GetUser(int id)
    {
        var user = await _userRepository.GetUser(id);

        var userToReturn = _mapper.Map<UserForDetailDto>(user);

        return Ok(userToReturn);
    }



    [HttpGet("{id}/favouriteRecipes")] 
    public async Task<IActionResult> GetUserFavRecipes(int id)
    {

        var userFavRecipes = await _userRepository.GetUserFavRecipes(id);

        return Ok(userFavRecipes);
    }
[HttpGet(“{id}”,Name=“GetUser”)]
公共异步任务GetUser(int-id)
{
var user=await\u userRepository.GetUser(id);
var userToReturn=\u mapper.Map(用户);
返回Ok(用户返回);
}
[HttpGet(“{id}/favoriteRecipes”)]
公共异步任务GetUserFavRecipes(int id)
{
var userFavRecipes=await_userRepository.GetUserFavRecipes(id);
返回Ok(userFavRecipes);
}
@利耶罗


我想问题出在dto映射器上:

mapper.Map<UserForDetailDto>(user);
mapper.Map(用户);

在这一行上放置一个断点,或者将用户对象写入某个日志,或者以其他方式检查它,然后检查FavRecipes属性

你是对的!我有不同的名字。最喜欢的食谱和FavRecipes。。我不知道它必须是相同的..它们不必是相同的,但您必须指定其他映射。顺便说一句,您可以非常轻松地为映射器编写单元测试。