在我的查询中,我使用QueryOver
遍历几个表。我遇到的问题是nHib
解析某些字段而不解析同一个表中的其他字段。这怎么可能?我将结果集投影到我的DTO
。
错误消息:could not resolve property: Rank.RankDefinition of: Model.Entities.Evaluation
同样,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;
}
}
分别从Rank和Stage表中获取RankID和StageID,但不获取* Definitions。
构建查询的最佳方法是什么?我已经尝试将代码分解为创建每个表的别名的部分,但仍然无法解决问题。
映射:
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();
}
}
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>();
}
}
答案 0 :(得分:3)
作为随机尝试,我将通过以下查询包含别名:
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>();