我花了好几个小时试图解决这个问题,包括浏览StackOverflow上所有以前提出的问题。
我试图通过EvaluationHeadId从TrainingCourse查询,这样可以正常工作,但是,我试图让它返回的TrainingRoute“无法解析属性:TrainingRoute.TrainingRouteDefinition:Model.Entities.TrainingCourse”
它保存完美,我真正的问题是查询。
更新为:
using (var session = SessionProvider.Instance.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
dto = session.QueryOver<TrainingCourse>()
.JoinQueryOver<EvaluationHead>(p => p.EvaluationHeads)
.JoinQueryOver<TrainingRoute>(p => p.TrainingRoute)
.Where(c => c.EvaluationHeadID == headId)
.SelectList(l => l
.Select(h => h.TrainingCourseDefn).WithAlias(() => d.TrainingCourseDefn)
.Select(h => h.IsAvailable).WithAlias(() => d.IsAvailable)
.Select(h => h.TrainingRoute.TrainingRouteDefinition).WithAlias(() => d.TrainingRouteDefinition))
.TransformUsing(Transformers.AliasToBean<TrainingCourseDTO>())
.List<TrainingCourseDTO>();
transaction.Commit();
}
}
映射:
public TrainingCourseMap()
{
Id(x => x.TrainingCourseID).GeneratedBy.Identity();
Map(x => x.TrainingCourseDefn);
Map(x => x.IsAvailable);
Map(x => x.TrainingCourseCreatedBy);
Map(x => x.TrainingCourseDtCreation);
Map(x => x.TrainingCourseDtModified);
Map(x => x.TrainingCourseModifiedBy);
References(x => x.TrainingRoute).Column("TrainingRouteID").Cascade.None();
HasManyToMany(x => x.EvaluationHeads).Table("EvaluationTraining").ParentKeyColumn("TrainingCourseID").ChildKeyColumn("EvaluationHeadID").Inverse().Cascade.All();
}
public EvaluationHeadMap()
{
Id(x => x.EvaluationHeadID).GeneratedBy.Identity();
Map(x => x.ManagerID);
Map(x => x.SupervisorID);
Map(x => x.EvaluationStartPeriod);
Map(x => x.EvaluationEndPeriod);
Map(x => x.EmployeeScalePoint);
Map(x => x.KRASignature);
Map(x => x.KRASignatureDate);
Map(x => x.DateCreated);
Map(x => x.DateModified);
HasMany(x => x.KeyResultAreas).KeyColumn("EvaluationHeadID").Cascade.All().Inverse();
HasMany(x => x.Evaluations).KeyColumn("EvaluationHeadID").Inverse().Cascade.All();
HasManyToMany(x => x.TrainingCourses).Table("EvaluationTraining").ParentKeyColumn("EvaluationHeadID").ChildKeyColumn("TrainingCourseID").Cascade.All().AsBag();
References(x => x.Stage).Column("StageID").Cascade.None();
References(x => x.Employee).Column("EmployeeID").Cascade.None();
References(x => x.Employment).Column("EmploymentID").Cascade.None();
//References(x => x.Manager).Column("EmployeeID");
//References(x => x.Supervisor).Column("EmployeeID");
}
public TrainingRouteMap()
{
Id(x => x.TrainingRouteID).GeneratedBy.Identity();
Map(x => x.TrainingRouteDefinition);
Map(x => x.TrainingRouteDescription);
HasMany(x => x.TrainingCourses).KeyColumn("TrainingRouteID").Cascade.AllDeleteOrphan().Inverse();
}
注意:我在TrainingCourse和TrainingRoute之间有另一个查询,它根本没有提出任何问题,甚至通过TrainingCourse.TrainingRoute.x模式访问属性。与此问题的唯一区别在于我也在查询其他表格。
答案 0 :(得分:3)
TrainingRoute
是TrainingCourse
(以及EvaluationHeads
)的引用属性。所以你必须使用JoinQueryOver或JoinAlias。下面我们将创建用于别名的虚拟对象(全部设置为null)。我们还拆分了查询的加入,因为它们会导致对新创建查询的引用
TrainingCourse trainingCourse = null;
TrainingRoute trainingRoute = null;
EvaluationHead evaluationHead = null;
var query = session.QueryOver<TrainingCourse>(() => trainingCourse);
// here we can work with criteria against the TrainingRoute
var referenceToTraingRouteQuery = query
.JoinQueryOver<TrainingRoute>(p => p.TrainingRoute, () => trainingRoute);
// here we can filter the EvaluationHead collection
var referenceToEvaluationHeadQuery = query // here we start again from the base query
.JoinQueryOver<EvaluationHead>(p => p.EvaluationHeads, () => evaluationHead)
.Where(c => c.EvaluationHeadID == headId);
dto = query
.SelectList(l => l
.Select(() => trainingCourse.TrainingCourseDefn)
.WithAlias(() => d.TrainingCourseDefn)
.Select(() => trainingCourse.IsAvailable)
.WithAlias(() => d.IsAvailable)
// now let's used join alias
.Select(() => trainingRoute.TrainingRouteDefinition)
.WithAlias(() => d.TrainingRouteDefinition))
.TransformUsing(Transformers.AliasToBean<TrainingCourseDTO>())
.List<TrainingCourseDTO>();
或者您可以使用JoinAlias,请点击此处16.4. Associations