0

我有一个类(项目),它代表一个有点大的数据库表。除了一个子类(ProjectUses,它代表另一个与 Project 具有 1:1 关系的数据库表)之外,我只需要检索大约十几个列。我可以使用如下所示的投影很好地获得 Project 对象的单列,但是在检索 Project 对象并填充 ProjectUses 子类时也遇到了问题。谁能指出我正确的方向?

这是我正在使用的存储库代码:

public Project GetProjectDebenture(int pkProjectID)
{
    var proj = _session.CreateCriteria(typeof(Project))
                .SetProjection(Projections.ProjectionList()
                    .Add(Projections.Property("pkProjectID"), "pkProjectID")
                    .Add(Projections.Property("ReservePercentage"), "ReservePercentage")
                    .Add(Projections.Property("FundingFeePercentage"), "FundingFeePercentage")
                    .Add(Projections.Property("PackagingFeePercentage"), "PackagingFeePercentage")
                    )
                .SetResultTransformer(Transformers.AliasToBean(typeof(Project)))
                .Add(Restrictions.Eq("pkProjectID", pkProjectID))
                .UniqueResult<Project>();
            return proj;
        }
        return null;
    }

我尝试使用Projections.Property("ProjectUses")Projections.GroupProperty("ProjectUses")检索填充的子类,但没有成功。

下面是类和映射定义的[简化]代码

// Project class
public partial class Project
{
    public int pkProjectID { get; set; }
    public decimal ReservePercentage { get; set; }
    public decimal FundingFeePercentage { get; set; }
    public decimal PackagingFeePercentage { get; set; }
    public ProjectUses ProjectUses { get; set; }
}

// ProjectUses class
public partial class ProjectUses
{
    public int pkProjectID { get; set; }
    public Project Project { get; set; }
    ...
}

// Project mapping
public class ProjectMap : ClassMap<Project>
{
    public ProjectMap()
    {
        Table(@"Project");
        LazyLoad();
        Id(x => x.pkProjectID)
          .Column("pkProjectID")
          .Not.Nullable()
          .GeneratedBy.Identity();
        ...
        HasOne<ProjectUses>(x => x.ProjectUses)
          .PropertyRef(r => r.Project)
          .Cascade.All();
    }
}
// Project Uses mapping
public class ProjectUsesMap : ClassMap<ProjectUses>
{
    public ProjectUsesMap()
    {
        Table(@"ProjectUses");
        LazyLoad();
        Id(x => x.pkProjectID, "pkProjectID").GeneratedBy.Foreign("Project");
        HasOne<Project>(x => x.Project)
            .Constrained()
            .ForeignKey()
            .Cascade.None();

        ...
    }
}
4

2 回答 2

0

您可以制作一个具有您需要的所有属性的 DTO,ProjectProjectUses在新 DTO 上进行投影。这具有将视图模型与域模型分离的额外优势。

于 2013-03-25T22:25:50.680 回答
0

您是否尝试过.Not.LazyLoad()在 ProjectMap 类中使用?

于 2013-03-25T20:25:15.840 回答