Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#ASP.NET核心自动映射:子对象为空_C#_Asp.net Core_Automapper - Fatal编程技术网

C#ASP.NET核心自动映射:子对象为空

C#ASP.NET核心自动映射:子对象为空,c#,asp.net-core,automapper,C#,Asp.net Core,Automapper,我在用户和帐户之间有一种多对多的关系 当我执行HttpGet时,我想返回AccountForDetailDto,其中包括一个ICollection,以列出附加到该帐户的用户 我获取附加到帐户的用户的用户ID,但用户对象为空 我假设我需要在某个地方执行一个包含,但我不确定在哪里。我尝试了.Include(account=>account.UserAccounts.Select(UserAccounts=>UserAccounts.User) 或者我缺少地图/地图的属性 以下是我得到的: Accou

我在用户和帐户之间有一种多对多的关系

当我执行
HttpGet
时,我想返回
AccountForDetailDto
,其中包括一个
ICollection
,以列出附加到该帐户的用户

我获取附加到帐户的用户的用户ID,但用户对象为空

我假设我需要在某个地方执行一个
包含
,但我不确定在哪里。
我尝试了
.Include(account=>account.UserAccounts.Select(UserAccounts=>UserAccounts.User)

或者我缺少地图/地图的属性

以下是我得到的:

AccountsController.cs
[HttpGet(“{id}”,Name=“GetAccount”)]
公共异步任务GetAccount(int id)
{
var accountFromRepo=等待_repo.GetAccount(id);
var accountToReturn=\u mapper.Map(accountFromRepo);
返回Ok(accountToReturn);
}

AccountRepository.cs

public异步任务GetAccount(int-id)
{
return wait_context.Accounts
.Include(a=>a.UserAccounts)
.FirstOrDefaultAsync(a=>a.Id==Id);
}

AutoMapperProfiles.cs

CreateMap();
CreateMap()
.ForMember(dto=>dto.Users,opt=>opt.MapFrom(a=>a.UserAccounts));

模型

公共类帐户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection用户帐户{get;set;}
}
公共类用户
{
公共int Id{get;set;}
公共字符串用户名{get;set;}
公共ICollection用户帐户{get;set;}
}
公共类用户帐户
{
public int?UserId{get;set;}
公共用户{get;set;}
public int?AccountId{get;set;}
公共帐户{get;set;}
}

Dtos

公共类AccountForDetailedTo
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection用户{get;set;}
}
公共类UserForListDto
{
公共int Id{get;set;}
公共字符串用户名{get;set;}
}

似乎第一步是做
。然后在
GetAccount(int-id)
中包含(ua=>ua.User)
。但是我仍然没有在返回的
AccountForDetailedDto
中获得用户列表,然后需要将AutoMapper配置文件更改为
.formmember(dto=>dto.users,opt=>opt.MapFrom)(a=>a.UserAccounts.Select(ua=>ua.User));
我不需要在AutoMapper配置文件中使用
CreateMap();
[HttpGet("{id}", Name = "GetAccount")]
public async Task<IActionResult> GetAccount(int id)
{
    var accountFromRepo = await _repo.GetAccount(id);

    var accountToReturn = _mapper.Map<AccountForDetailedDto>(accountFromRepo);

    return Ok(accountToReturn);
}
public async Task<Account> GetAccount(int id)
{
    return await _context.Accounts
        .Include(a => a.UserAccounts)
        .FirstOrDefaultAsync(a => a.Id == id);
}
CreateMap<UserAccount, UserForListDto>();

CreateMap<Account, AccountForDetailedDto>()
    .ForMember(dto => dto.Users, opt => opt.MapFrom(a => a.UserAccounts));
public class Account
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<UserAccount> UserAccounts { get; set; }
}

public class User
{
    public int Id { get; set; }

    public string Username { get; set; }

    public ICollection<UserAccount> UserAccounts { get; set; }
}

public class UserAccount
{
    public int? UserId { get; set; }
    public User User { get; set; }

    public int? AccountId { get; set; }
    public Account Account { get; set; }
}
public class AccountForDetailedDto
{
    public int Id { get; set; }

    public string Name { get; set; }

    public ICollection<UserForListDto> Users { get; set; }
}

public class UserForListDto
{
    public int Id { get; set; }

    public string Username { get; set; }
}