将DTO映射到ViewModel

时间:2017-10-25 14:03:09

标签: c# asp.net-mvc entity-framework automapper

我有一个ASP.NET MVC n层分层应用程序,我正在尝试将我从查询中获取的DTO映射到viewModel但是我收到以下错误:"指定的类型成员' TrainingNo' LINQ to Entities不支持。仅支持初始化程序,实体成员和实体导航属性。"在这行代码中:

IQueryable<TraineeViewModel> certificates = certs.ProjectTo<TraineeViewModel>();

如果证书提供了TraineeDto的IQueryable,而TrainingNo是两个类中的字符串,定义为:

public string TrainingNo { get; set; }

在谷歌搜索或搜索StackOverflow上没什么帮助。希望有人对此错误有一些经验,但如果需要,我可以提供更多代码详细信息。谢谢!

编辑: S.Akbari你提到的问题没有说明这是AutoMapper的一个问题。我可以从我在DB上的查询中将查询作为DTo返回,但问题是从DTO映射到ViewModel。如果你认为你指出的问题答案我的问题,请你明确指出吗?阅读完解决方案后,它无法解决我的问题。感谢

编辑2: 映射配置:

public static void RegisterMaps()
    {
        AutoMapper.Mapper.Initialize(config =>
        {
            config.CreateMap<TraineeDto, TraineeViewModel>();
            config.CreateMap<TraineeViewModel, TraineeDto>();

        });
    }

ViewModel:

public class TraineeViewModel
{
    public int Id { get; set; }
    public int? RigId { get; set; }
    public string RigName { get; set; }
    public int? PositionId { get; set; }
    public string PositionName { get; set; }
    public string RigAndPosition { get; set; }
    public string TrainingNo { get; set; }
    public string Title { get; set; }
    public DateTime TimeLimit { get; set; }
    public string Trainer { get; set; }
    public string TrainerSignature { get; set; }
    public DateTime? SignedOn { get; set; }
    public string Certifier { get; set; }
    public DateTime? CertifiedOn { get; set; }
    public DateTime? ValidUntil { get; set; }
    public string Alert { get; set; }
    public TrainingStatus Status { get; set; }

}

实体类:

public class Certificate 
{        
    public DateTime? CreatedDate { get; set; }

    [ForeignKey("Candidate")]
    public int? CandidateId { get; set; }
    public virtual Person Candidate { get; set; }

    [ForeignKey("Trainer")]
    public int? TrainerId { get; set; }
    public virtual Person Trainer { get; set; }

    [ForeignKey("MasterCertificate")]
    public int? MasterCertificateId { get; set; }
    public virtual MasterCertificate MasterCertificate { get; set; }

    [ForeignKey("Certifier")]
    public int? CertifierId { get; set; }
    public virtual Person Certifier { get; set; }

    public DateTime? Validity { get; set; }

    public bool? NeedReCertification { get; set; }

    public DateTime? ExpiryDate { get; set; }

    [InverseProperty("Certificate")]
    public virtual ICollection<Training> Trainings { get; set; }

    [InverseProperty("Certificate")]
    public virtual ICollection<CertificationSignature> CertificationSignatures { get; set; }                
}

存储库查询:

public IQueryable<Certificate> GetCertificatesForPerson(int candidateId)
    {
        return _context.Certificates.Where(ts => ts.CandidateId == candidateId);
    }

服务层查询:

public IQueryable<TraineeDto> GetCertificatesForUser(int userId)
    {
        var query = _unitOfWork.Certificates.GetCertificatesForPerson(userId);

        var certificates = query.Select(x => new TraineeDto
        {
            Id = x.Id,
            RigId = x.MasterCertificate.FacilityId,
            RigName = (x.MasterCertificate.FacilityId != null) ? x.MasterCertificate.Facility.Name : "",
            PositionId = x.MasterCertificate.PositionId,
            PositionName = (x.MasterCertificate.PositionId != null) ? x.MasterCertificate.Position.RoleName : "",
            RigAndPosition = ((x.MasterCertificate.FacilityId != null) ? x.MasterCertificate.Facility.Name : "")
                             + " - " +
                             ((x.MasterCertificate.PositionId != null) ? x.MasterCertificate.Position.RoleName : ""),


            Certifier = x.Certifier.FirstName, //+ ((x.Certifier.MiddleName != null) ? (" " + x.Certifier.MiddleName) : "" ) + " " + x.Certifier.LastName,
            //CertifiedOn = (x.CertificationSignatures != null && x.CertificationSignatures.Any()) ? x.CertificationSignatures.Last().CertificateSignedOn : (DateTime?)null,
            //ValidUntil = (x.CertificationSignatures != null && x.CertificationSignatures.Any()) ? x.CertificationSignatures.Last().InactiveDate : (DateTime?)null,
            //Alert = x.


        });

        return certificates;
    }

0 个答案:

没有答案