Question

In my query, I am traversing a few tables using QueryOver. The issue that I am having is that nHib resolves some fields while not resolving others in the same table. How is this possible? I am projecting the result set into my DTO.

Error message: could not resolve property: Rank.RankDefinition of: Model.Entities.Evaluation likewise, similar message for StageDefinition

public static EvaluationDTO GetByHeadId(long headId)
{
    EvaluationDTO dto = new EvaluationDTO();

    Evaluation evaluation = null;

    try
    {
        using (var session = SessionProvider.Instance.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {
                dto = session.QueryOver<Evaluation>(() => evaluation)
                            .Where(() => evaluation.EvaluationHead.EvaluationHeadID == headId)
                            .JoinQueryOver(() => evaluation.Rank)
                            .SelectList(l => l
                                .Select(h => h.EvaluationID).WithAlias(() => dto.EvaluationID)
                                .Select(h => h.EvaluationHead.EvaluationHeadID).WithAlias(() => dto.EvaluationHeadID)
                                .Select(h => h.EvaluationTotalScore).WithAlias(() => dto.EvaluationTotalScore)                                                                             
                                .Select(h => h.Rank.RankID).WithAlias(() => dto.RankID)
   /*does not resolve >> */     //.Select(h => h.Rank.RankDefinition).WithAlias(() => dto.RankDefinition)
                                .Select(h => h.Stage.StageID).WithAlias(() => dto.StageID)
   /*does not resolve >> */     //.Select(h => h.Stage.StageDefinition).WithAlias(() => dto.StageDefinition)
                                .Select(h => h.EvaluationDtCreated).WithAlias(() => dto.EvaluationDtCreation))
                            .TransformUsing(Transformers.AliasToBean(typeof(EvaluationDTO)))
                            .SingleOrDefault<EvaluationDTO>();

                transaction.Commit();

                return dto;
            }                    
        }
    }
    catch (Exception)
    {
        throw;
    }
}

In gets both RankID and StageID from the Rank and Stage tables respectively but not the *Definitions.

Whats the best way going about constructing the query? I have already tried breaking the code into parts creating aliases of each table but that still does not correct the problem.

Mappings:

public class EvaluationMap : ClassMap<Evaluation>
    {
        public EvaluationMap()
        {
            Id(x => x.EvaluationID).GeneratedBy.Identity();
            Map(x => x.EvaluationTotalScore);
            Map(x => x.EvaluationDtCreated);
            Map(x => x.EvaluationDtModified);
            Map(x => x.PERApprovedBy);
            Map(x => x.PERApprovedDate);

            References(x => x.EvaluationHead).Column("EvaluationHeadID").Cascade.None();
            References(x => x.Rank).Column("RankID").Cascade.None();            
            References(x => x.Stage).Column("StageID").Cascade.None();

            References(x => x.EvaluationReason).Column("EvaluationReasonID").Cascade.None();
            References(x => x.EvaluationRecommendation).Column("EvaluationRecommendationID").Cascade.None();
            References(x => x.EvaluationAction).Column("EvaluationActionID").Cascade.None();

            HasMany(x => x.EvaluationSignatures).KeyColumn("EvaluationID").Cascade.All().Inverse().AsBag();
            HasMany(x => x.EvaluationFactors).KeyColumn("EvaluationID").Cascade.AllDeleteOrphan().Inverse().AsBag();
            HasMany(x => x.Comments).KeyColumn("EvaluationID").Cascade.AllDeleteOrphan().Inverse().AsBag();
            HasMany(x => x.PerformanceDevelopments).KeyColumn("EvaluationID").Cascade.All().Inverse().AsBag();
        }
    }

public class EmployeeEvaluationRankMap : ClassMap<EmployeeEvaluationRank>
    {
        public EmployeeEvaluationRankMap()
        {
            Id(x => x.RankID).GeneratedBy.Identity();
            Map(x => x.RankDefinition);
            Map(x => x.RankPercentLowerBound);
            Map(x => x.RankPercentUpperBound);
            Map(x => x.EffectiveDate);
            Map(x => x.ExpiryDate);
            Map(x => x.RankVersion);
            Map(x => x.RankDtCreation);
            Map(x => x.RankDtModified);
            HasMany(x => x.Evaluations).KeyColumn("RankID").Inverse().AsBag();
        }
    }

Entities

public class Evaluation
{

    public virtual long EvaluationID { get; set; }
    public virtual EvaluationHead EvaluationHead { get; set; }
    public virtual double EvaluationTotalScore { get; set; }
    public virtual EmployeeEvaluationRank Rank { get; set; }
    public virtual Stage Stage { get; set; }
    public virtual DateTime EvaluationDtCreated { get; set; }
    public virtual DateTime? EvaluationDtModified { get; set; }
    public virtual long? PERApprovedBy { get; set; }
    public virtual DateTime? PERApprovedDate { get; set; }

    public virtual EvaluationReason EvaluationReason { get; set; }
    public virtual EvaluationRecommendation EvaluationRecommendation { get; set; }
    public virtual EvaluationAction EvaluationAction { get; set; }

    public virtual ICollection<Comment> Comments { get; set; }
    public virtual ICollection<EvaluationFactor> EvaluationFactors { get; set; }
    public virtual ICollection<EvaluationSignature> EvaluationSignatures { get; set; }
    public virtual ICollection<PerformanceDevelopment> PerformanceDevelopments { get; set; }

    public Evaluation()
    {
        Comments = new List<Comment>();
        EvaluationFactors = new List<EvaluationFactor>();
        EvaluationSignatures = new List<EvaluationSignature>();
        PerformanceDevelopments = new List<PerformanceDevelopment>();
    }
}

public class EmployeeEvaluationRank
{

    public virtual int RankID { get; set; }
    public virtual string RankDefinition { get; set; }
    public virtual float RankPercentLowerBound { get; set; }
    public virtual float RankPercentUpperBound { get; set; }
    public virtual DateTime EffectiveDate { get; set; }
    public virtual DateTime? ExpiryDate { get; set; }
    public virtual int RankVersion { get; set; }
    public virtual DateTime RankDtCreation { get; set; }
    public virtual DateTime? RankDtModified { get; set; }
    public virtual ICollection<Evaluation> Evaluations { get; set; }

    public EmployeeEvaluationRank()
    {
        Evaluations = new List<Evaluation>();
    }
}
Était-ce utile?

La solution

As a random try, I would include aliases through the following query :

EmployeeEvaluationRank rankAlias = null;
Stage stageAlias = null;

dto = session.QueryOver<Evaluation>(() => evaluation)
        .Where(() => evaluation.EvaluationHead.EvaluationHeadID == headId)
        .JoinQueryOver(() => evaluation.Rank, () => rankAlias)
        .JoinQueryOver(() => evaluation.Stage, () => stageAlias)
        .SelectList(l => l
            .Select(h => h.EvaluationID).WithAlias(() => dto.EvaluationID)
            .Select(h => h.EvaluationHead.EvaluationHeadID).WithAlias(() => dto.EvaluationHeadID)
            .Select(h => h.EvaluationTotalScore).WithAlias(() => dto.EvaluationTotalScore)
            .Select(h => h.Rank.RankID).WithAlias(() => dto.RankID)
            .Select(h => rankAlias.RankDefinition).WithAlias(() => dto.RankDefinition)
            .Select(h => h.Stage.StageID).WithAlias(() => dto.StageID)
            .Select(h => stageAlias.StageDefinition).WithAlias(() => dto.StageDefinition)
            .Select(h => h.EvaluationDtCreated).WithAlias(() => dto.EvaluationDtCreation))
        .TransformUsing(Transformers.AliasToBean(typeof(EvaluationDTO)))
        .SingleOrDefault<EvaluationDTO>();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top