MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.38k stars 331 forks source link

EFcore uses ProjectToType mapping errors #690

Closed SpiderMan95 closed 7 months ago

SpiderMan95 commented 7 months ago

The following example correctly maps the result I want

public async Task<List<MenuDto>> GetRoleMenuTreeListAsync(long roleId)
{
var test = await Context.RoleMenus.Where(x => x.RoleId == roleId && !x.Menu.ParentId.HasValue)
.Include(x => x.Menu)
.ThenInclude(x => x.Childs.Where(m => m.RoleMenus.Any(r => r.RoleId == roleId)))
.Select(x => x.Menu)
.ToListAsync();

return mapper.Map<List<MenuDto>>(test);
}

The correct result is shown below

{
  "code": 200,
  "status": true,
  "msg": "",
  "response": [
    {
      "id": 561538574873448450,
      "menuName": "5",
      "path": "5",
      "parentId": null,
      "menuIcon": "6",
      "sortNumber": 1,
      "isDisable": false,
      "parent": null,
      "childs": [
        {
          "id": 561539079016206340,
          "menuName": "4",
          "path": "4",
          "parentId": 561538574873448450,
          "menuIcon": "5",
          "sortNumber": 1,
          "isDisable": false,
          "parent": null,
          "childs": null
        }
      ]
    },
    {
      "id": 561538830952484860,
      "menuName": "2",
      "path": "2",
      "parentId": null,
      "menuIcon": "3",
      "sortNumber": 3,
      "isDisable": false,
      "parent": null,
      "childs": []
    }
  ]
}

But when I used the ProjectToType mapping I got exactly the wrong result, as shown below

    public async Task<List<MenuDto>> GetRoleMenuTreeListAsync(long roleId)
    {
        return await Context.RoleMenus.Where(x => x.RoleId == roleId&&!x.Menu.ParentId.HasValue)
            .Include(x => x.Menu)
            .ThenInclude(x => x.Childs.Where(m => m.RoleMenus.Any(r => r.RoleId == roleId)))
            .Select(x => x.Menu)
            .ProjectToType<MenuDto>()
            .ToListAsync();
    }
}

The error results are as follows

{
  "code": 200,
  "status": true,
  "msg": "",
  "response": [
    {
      "id": 561538574873448450,
      "menuName": "5",
      "path": "5",
      "parentId": null,
      "menuIcon": "6",
      "sortNumber": 1,
      "isDisable": false,
      "parent": null,
      "childs": [
        {
          "id": 561539079016206340,
          "menuName": "4",
          "path": "4",
          "parentId": 561538574873448450,
          "menuIcon": "5",
          "sortNumber": 1,
          "isDisable": false,
          "parent": null,
          "childs": null
        }
      ]
    },
    {
      "id": 561538830952484860,
      "menuName": "2",
      "path": "2",
      "parentId": null,
      "menuIcon": "3",
      "sortNumber": 3,
      "isDisable": false,
      "parent": null,
      "childs": [
        {
          "id": 561551620509478900,
          "menuName": "8",
          "path": "8",
          "parentId": 561538830952484860,
          "menuIcon": "8",
          "sortNumber": 1,
          "isDisable": false,
          "parent": null,
          "childs": null
        }
      ]
    }
  ]
}