在映射器配置文件中使用映射器

问题描述 投票:-1回答:1

我有一个组实体和一个特权实体,它们之间有多对多关系。

我想进行映射,发现自己在MapperConfigration类内进行了两个映射。

我使用AutoMapper第9版

有人可以帮我吗?

Group class

public partial class Group:IEntity
    {
        public Group()
        {
            GroupPrivilage = new HashSet<GroupPrivilage>();
            UserGroup = new HashSet<UserGroup>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Created { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? Modified { get; set; }
        public string ModifiedBy { get; set; }

        public virtual ICollection<GroupPrivilage> GroupPrivilage { get; set; }
        public virtual ICollection<UserGroup> UserGroup { get; set; }
    }

特权级别

public partial class Privilage:IEntity
    {
        public Privilage()
        {
            GroupPrivilage = new HashSet<GroupPrivilage>();
        }

        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public DateTime Created { get; set; }
        public string CreatedBy { get; set; }
        public string ModifiedBy { get; set; }
        public DateTime? Modified { get; set; }

        public virtual ICollection<GroupPrivilage> GroupPrivilage { get; set; }
    }

Group DTO

public class GroupResopnseDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Created { get; set; }
        public string CreatedBy { get; set; }
        public DateTime? Modified { get; set; }
        public string ModifiedBy { get; set; }
        public int PrivilagesCount { set; get; }
        public int UserCount { set; get; }
        public List<PrivelegGroupDTO> Privilages { set; get; }
    }

PrivelegGroupDTO

public class PrivelegGroupDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }

MapperConfigration

public class MapperConfigration : Profile
    {
        IMapper _mapper;
        public MapperConfigration(IMapper mapper)
            => _mapper = mapper;
        public MapperConfigration()
        {
            CreateMap<User, UserLoginDTO>();
            CreateMap<UserLoginDTO, User>();
            CreateMap<Privilage, PrivelegGroupDTO>();
            CreateMap<Group, GroupResopnseDTO>()
                .ForMember(r => r.PrivilagesCount, opt => opt.MapFrom(g => g.GroupPrivilage.Count))
                .ForMember(r => r.UserCount, opt => opt.MapFrom(g => g.UserGroup.Count))
                .ForMember(r => r.Privilages, opt => opt.MapFrom(g => _mapper.Map<PrivelegGroupDTO>(g.GroupPrivilage.Select(c => c.Privilage))));
        }

    }

启动

services.AddAutoMapper(typeof(Startup));

它不会引发错误,但是特权得到一个空数组

c# dependency-injection dto mapper
1个回答
0
投票

我使用隐式运算符解决它所以这是真的谢谢

public class PrivelegGroupDTO
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }

        public static implicit operator PrivelegGroupDTO(Privilage privilage)
        {
            return new PrivelegGroupDTO() { Id = privilage.Id, Name = privilage.Name, Description = privilage.Description };
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.