.net 无法在复杂的自动Map中获得输出

ycl3bljg  于 2023-06-25  发布在  .NET
关注(0)|答案(1)|浏览(107)

我有三个数据库实体表角色、特征、角色特征

`Public class Roles
{
        public long RoleIdPk { get; set; }
        public string RoleName { get; set; }
        public string RoleDescription{ get; set; }
        public int CreatedBy{ get; set; }
}
Public class Features
{
        public long FeatureIDPk{ get; set; }
        public string FeatureName{ get; set; }
        public string FeatureDescription{ get; set; }
        public int CreatedBy{ get; set; }
}
Public class RoleFeatures
{
        public long RoleIDFk{ get; set; }
        public long FeatureIDFk{ get; set; }
        public string ViewPermission{ get; set; }
        public string addPermission{ get; set; }
        public stringEditPermission{ get; set; }
        public string DeletePermission{ get; set; }
}`

我想得到一个输出,就像在所有GetAllRoles API中我想要的输出

`"status": "true",
    "data": {
“roles”:[
{                       "roleId": "number",
            "roleName": "string",
            "features":[
            {
            “featureId”:”number”,
            “featureName”:”string”,
            "view": "string",
                        "add": "string",
            "edit": "string",
            "delete": "string"
            },
            {
            “featureId”:”number”,
            “featureName”:”string”,
            "view": "string",
                        "add": "string",
            "edit": "string",
            "delete": "string"
            },  
            ]
            }
}`
public class GetRolesResponse : FeaturesDTO
    {
        public long RoleId { get; set; }
        public string RoleName { get; set; }
       public List<FeaturesDTO> Features { get; set; }
    }
    public class FeaturesDTO 
    {
        public long FeatureId { get; set; }
        public string FeatureName { get; set; }
        public bool ViewPerm { get; set; }
        public bool AddPerm { get; set; }
        public bool EditPerm { get; set; }
        public bool DeletePerm { get; set; }

    }
}

角色MapCreateMap<Roles, GetRolesResponse>();列出roles 1 = _mapper.Map<List>(roles);
GeallRoles查询
List<Role>roles = await _context.Roles.ToListAsync();
但是在getall方法的角色中我没有得到特征表的数组。我只得到RoleDTO与值。该怎么办呢?

nwnhqdif

nwnhqdif1#

您的Roles型号没有与GetRolesResponse中的Features匹配的相应List,因此Features将为null。
你能改变RoleFeaturesFeaturesDTO的模型结构,使两者匹配吗?如果你可以的话,它将非常简单。下面是一个例子:
型号:

public class Roles
{
    [Key]
    public long RoleIdPk { get; set; }
    public string RoleName { get; set; }
    public string RoleDescription { get; set; }
    public int CreatedBy { get; set; }
    public List<RoleFeatures> RoleFeatures { get; set; }
}

public class Features
{
    [Key]
    public long FeatureIDPk { get; set; }
    public string FeatureName { get; set; }
    public string FeatureDescription { get; set; }
    public int CreatedBy { get; set; }
}
public class RoleFeatures
{
    public long RoleIDFk { get; set; }
    [Key]
    public long FeatureIDFk { get; set; }
    public string FeatureName { get; set; }
    public string ViewPermission { get; set; }
    public string addPermission { get; set; }
    public string EditPermission{ get; set; }
    public string DeletePermission { get; set; }
    [ForeignKey("RoleIDFk")]
    [JsonIgnore]
    public Roles Roles { get; set; }
}

public class GetRolesResponse
{
    public long RoleId { get; set; }
    public string RoleName { get; set; }
    public List<FeaturesDTO> Features { get; set; }
}
public class FeaturesDTO
{
    public long FeatureId { get; set; }
    public string FeatureName { get; set; }
    public string ViewPerm { get; set; }
    public string AddPerm { get; set; }
    public string EditPerm { get; set; }
    public string DeletePerm { get; set; }
}

公司简介:

public class MyProfile : Profile
{
    public MyProfile()
    {
        CreateMap<Roles, GetRolesResponse>()
            .ForMember(dest =>
                dest.RoleId,
                opt => opt.MapFrom(src => src.RoleIdPk))
            .ForMember(dest =>
                dest.Features,
                opt => opt.MapFrom(src => src.RoleFeatures));

        CreateMap<RoleFeatures, FeaturesDTO>()
            .ForMember(dest =>
                dest.FeatureId,
                opt => opt.MapFrom(src => src.FeatureIDFk))
            .ForMember(dest =>
                dest.ViewPerm,
                opt => opt.MapFrom(src => src.ViewPermission))
            .ForMember(dest =>
                dest.EditPerm,
                opt => opt.MapFrom(src => src.EditPermission))
            .ForMember(dest =>
                dest.AddPerm,
                opt => opt.MapFrom(src => src.addPermission))
            .ForMember(dest =>
                dest.DeletePerm,
                opt => opt.MapFrom(src => src.DeletePermission));
    }
}

控制器:

public async Task<List<GetRolesResponse>> IndexAsync()
{
    List<Roles> roles = await _context.Roles.Include(r => r.RoleFeatures).ToListAsync();
    var roles1 = _mapper.Map<List<GetRolesResponse>>(roles);
    return roles1;
}

测试结果:

相关问题