AutoMapper 的 EF Core 相关数据为空

EF Core related data is null with AutoMapper

在我的应用程序中,一个案例可以有很多公司。

我的模特:

public class Case
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public IList<CaseCompany> CaseCompanies { get; set; }
}

public class CaseInput
{
    public Guid Id { get; set; }
    [Required]
    public string Name { get; set; }
    public IList<CaseCompanyInput> CaseCompanyInputs { get; set; }
}

public class Company
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public string Name { get; set; }
}

public class CompanyInput
{
    public Guid Id { get; set; }
    [Required]
    public string Name { get; set; }
}

public class CaseCompany
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }
    public Guid CaseId { get; set; }
    public Guid CompanyId { get; set; }
    public class Case Case { get; set; }
    public class Company Company { get; set; }
}

public class CaseCompanyInput
{
    public Guid Id { get; set; }
    public Guid CaseId { get; set; }
    public Guid CompanyId { get; set; }
    public class CaseInput CaseInput { get; set; }
    public class CompanyInput CompanyInput { get; set; }
}

AutoMapperProfiles.cs:

// In Startup.cs: services.AddAutoMapper(typeof(AutoMapperProfiles));

public class AutoMapperProfiles : Profile
{
    public AutoMapperProfiles()
    {
        CreateMap<Case, CaseInput>().ReverseMap();
        CreateMap<CaseCompany, CaseCompanyInput>().ReverseMap();
        CreateMap<Company, CompanyInput>().ReverseMap();
    }
}

EditCase.cs:

private readonly DBContext _dbContext;
private readonly IMapper _mapper;

[BindProperty]
public CaseInput CaseInput { get; set; }

public EditCase(DBContext dbContext, IMapper mapper)
{
    _dbContext = dbContext;
    _mapper = mapper;
}

public async Task<IActionResult> OnGetAsync(Guid caseId)
{
    var getCase = await _dbContext.Cases.Include(x => x.CaseCompanies).ThenInclude(x => x.Company).FirstOrDefaultAsync(x => x.Id == caseId);
    CaseInput = _mapper.Map<CaseInput>(getCase);

    Console.WriteLine(getCase.CaseCompanies[0].Company.Name) // gets the company name
    Console.WriteLine(CaseInput.CaseCompanyInputs[0].CompanyInput.Name) // CaseInput.Name is not null but CaseCompanyInputs is null

    return Page();

}

我也试过了:

CaseInput = await _dbContext.Cases.ProjectTo<CaseInput>(_mapper.ConfigurationProvider).FirstOrDefaultAsync(x => x.Id == caseId);

CaseInput = await _dbContext.Cases.Include(x => x.CaseCompanies).ThenInclude(x => x.Company).ProjectTo<CaseInput>(_mapper.ConfigurationProvider).FirstOrDefaultAsync(x => x.Id == caseId);

结果相同:CaseCompanyInputs 为空。

我最好的猜测是输入模型的关系存在错误,但我就是看不到它。我相信我已经遵循命名约定来使关系正确。

如有任何帮助,我们将不胜感激。

要使自动映射工作,您必须将目标 属性 名称重命名为与源 属性 名称相同的名称

public class CaseInput
{
    public Guid Id { get; set; }
    public string Name { get; set; }
    //public IList<CaseCompanyInput> CaseCompanyInputs { get; set; }
    public IList<CaseCompanyInput> CaseCompanies { get; set; }
}

public class CaseCompanyInput
{
    public Guid Id { get; set; }
    public Guid CaseId { get; set; }
    public Guid CompanyId { get; set; }
    //public CaseInput CaseInput { get; set; }
    public CaseInput Case { get; set; }
    //public CompanyInput CompanyInput { get; set; }
    public CompanyInput Company { get; set; }
}

如果您不想更改 属性 名称,请将您的配置更改为以下内容。

CreateMap<Case, CaseInput>()
    .ForMember(dest => dest.CaseCompanyInputs, opt => opt.MapFrom(src => src.CaseCompanies));

CreateMap<CaseCompany, CaseCompanyInput>()
    .ForMember(dest => dest.CaseInput, opt => opt.MapFrom(src => src.Case))
    .ForMember(dest => dest.CompanyInput, opt => opt.MapFrom(src => src.Company));