gpt4 book ai didi

c# - nHibernate:无法解析属性

转载 作者:行者123 更新时间:2023-11-30 22:11:37 27 4
gpt4 key购买 nike

我花了几个小时试图解决这个问题,包括在 StackOverflow 上浏览所有之前提出的问题。

我正在尝试通过 EvaluationHeadId 从 TrainingCourse 查询,这工作正常,但是,我尝试获取它返回的 TrainingRoute“无法解析属性:TrainingRoute.TrainingRouteDefinition of: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 模式访问属性。与这个的唯一区别是我也查询其他表。

最佳答案

TrainingRouteTrainingCourse(以及EvaluationHeads)的引用属性。因此,您还必须为此使用 JoinQueryOver 或 JoinAlias。下面我们将创建用于别名的虚拟对象(全部设置为空)。我们还拆分了查询的连接,因为它们会导致对新创建查询的引用

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

关于c# - nHibernate:无法解析属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20154567/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com