我如何创建一个流畅的 Nhibernate 映射,该映射具有多个 table 的连接,但 select 每个 table 只有 select 个列?

How do i create a fluent Nhibernate mapping having join of multiple tables but select only selective colums from each table?

我在数据库中有四个 tables

table年度
主要 - 密钥创建日期 DATETIME,
主要 - 密钥 ID INTEGER,
列 AnnualAmount,
年度货币列,
列 Column3,
列 Column4,
列 Column5,
列 Column6

table每月
主要 - 密钥创建日期 DATETIME,
主要 - 密钥 ID INTEGER,
列每月金额,
列每月货币,
列 Column31,
列 Column41,
列 Column51,

table股价
主要 - 密钥创建日期 DATETIME,
列 AverageSharevalueAmount,
列 CurrentSharevalue,

table其他详细信息
主要 - 密钥创建日期 DATETIME,
列客户类型,
列 clientName,
列 MarketValueAmount,
列客户地址,

我想加入所有四个 table 并且只从每个 table 中获取几列,例如 tableAnnual、MonthlyAmount 和 AnnualAmount 和 AnnualCurrency table每月的月度货币,tableSharevalue 的 CurrentSharevalue 和 "creationDate" 的 tableMiscDetails 的 MarketValueAmount WHERE Id = "XXXXXXX"

我已根据所有四个 tables

中所需的列,在 C# 代码中如下定义实体 class
public class ShareDetails
{
    public int Id { get; set; }
    public DateTime CreationDate { get; set; }
    public string AnnualAmount { get; set; }
    public string AnnualCurrency { get; set; }
    public string MonthlyAmount { get; set; }
    public string MonthlyCurrency { get; set; }
    public string CurrentSharevalue { get; set; }
    public string MarketValueAmount { get; set; }
}

How do I achieve the Mapping in Fluent Nhibernate ?

public class ShareDetailsMap : ClassMap<ShareDetails>
{

}

请注意,我只需要每个 table.Also 中的几列,我创建的实体 class 是否正常,还是我也需要更改其结构?

我需要做的就是通过 QueryOver、JoinAlias 执行以下查询

Select tableAnnual.AnnualAmount, tableAnnual.AnnualCurrency, tableMonthly.MonthlyAmount, MonthlyAmount.MonthlyCurrency, tableSharevalue.CurrentSharevalue、tableMiscDetails.clientType、tableMiscDetails.clientName、tableMiscDetails.MarketValueAmount 从 table 每年 外连接 table每月 tableAnnual.creationDate = tableMonthly.creationDate 外连接 table tableMonthly.creationDate 上的股票价值 = tableSharevalue.creationDate tableSharevalue.creationDate = tableMiscDetails.creationDate 上的外部联接 tableMiscDetails 其中 id = '123456'

假设您有 classes 和四个 tables 的映射,这可能适合您。正如上面的评论所述,我对下面的 ShareDetails 映射不是 100% 肯定,因为它没有连接到实际的数据库 table。

分享详情class:

public class ShareDetails
{
    public int Id { get; set; }
    public DateTime CreationDate { get; set; }
    public Annual Annual { get; set; }
    public Monthly Monthly { get; set; }
    public ShareValue ShareValue { get; set; }
    public MiscDetails MiscDetails { get; set; }
}

ShareDetails 映射:

public class ShareDetailsMap : ClassMap<ShareDetails>
    public ShareDetailsMap(){
        LazyLoad();
        //Don't think you need these since this isn't a db table
        //Id(x => x.DocumentId).GeneratedBy.Identity().Column("id");
        //Map(x => x.CreationDate).Column("creation_date");
        References(x => x.Annual).Column("creation_date");
        References(x => x.Monthly).Column("creation_date");
        References(x => x.ShareValue).Column("creation_date");
        References(x => x.MiscDetails).Column("creation_date");
    }
}

将所有随机属性拉入一个对象数组的查询:

Annual annualAlias = null;
Monthly monthlyAlias = null;
ShareValue shareAlias = null;
MiscDetails miscAlias = null;

// Create your db session...
using (session)
{
    return session.QueryOver<ShareDetails>()
        .JoinAlias(a => a.Annual, () => annualAlias)
        .JoinAlias(a => a.Monthly, () => monthlyAlias)
        .JoinAlias(a => a.ShareValue, () => shareAlias)
        .JoinAlias(a => a.MiscDetails, () => miscAlias)
        .SelectList(list => list
            .Select(() => annualAlias.AnnualAmount)
            .Select(() => annualAlias.AnnualCurrency)
            .Select(() => monthlyAlias.MonthlyAmount)
            .Select(() => monthlyAlias.MonthlyCurrency)
            .Select(() => shareAlias.CurrentShareValue)
            .Select(() => miscAlias.MarkeyValueAmount)
        ).List<object[]>();
}

Good reference on QueryOver

我自己解决了。以下是详情。

假设我有每个数据库的实体和映射 table。在其中一个实体中,假设 AnnualInformation 实体 (tableAnnual).

我将创建 MonthlyInformation 实体 (tableMonthly)、ShareValueInformation 实体 (tableSharevalue) 和 MiscDetails 实体 (tableMiscDetails) 的关系。

AnnualInformation 实体的代码如下所示:

public class AnnualInformation
{
    public virtual int Id { get; set; }
    public virtual DateTime CreationDate { get; set; }
    public virtual decimal AnnualAmount { get; set; }
    public virtual string AnnualCurrency { get; set; }
    public virtual MonthlyInformation MonthlyInformation { get; set; }
    public virtual ShareValueInformation ShareValueInformation { get; set; }
    public virtual MiscDetails MiscDetails { get; set; }
}

对应的映射class如下:

public class AnnualInformationMap : ClassMap<AnnualInformation>
{
    public AnnualInformationMap()
    {
        Table("tableAnnual");
        Id(x => x.CreationDate, "creationDate ");
        Map(x => x.Id, "Id");
        Map(x => x.AnnualAmount, "AnnualAmount");
        Map(x => x.AnnualCurrency, "AnnualCurrency");
        References(x => x.MonthlyInformation).Column("creationDate");
        References(x => x.ShareValueInformation).Column("creationDate");
        References(x => x.MiscDetails).Column("creationDate");
    }
}

现在,我们需要使用 Left Outer Join 从所有这四个数据库 table 中检索数据。因此我们将按如下方式解决

MonthlyInformation monthlyAlias = null;
ShareValueInformation shareAlias = null;
MiscDetails miscAlias = null;

// Create your db session...
using (session)
{
var result = session.QueryOver<AnnualInformation>()
             .JoinAlias(a => a.MonthlyInformation, () => monthlyAlias, JoinType.LeftOuterJoin)
             .JoinAlias(a => a.ShareValueInformation, () => shareAlias, JoinType.LeftOuterJoin)
             .JoinAlias(a => a.MiscDetails, () => miscAlias, JoinType.LeftOuterJoin)
             .SelectList(list => list
                         .Select(x => x.Id)
                         .Select(x => x.CreationDate)
                         .Select(x => x.AnnualAmount)
                         .Select(x => x.AnnualCurrency)
                         .Select(() => monthlyAlias.MonthlyAmount)
                         .Select(() => monthlyAlias.MonthlyCurrency)
                         .Select(() => shareAlias.CurrentSharevalue)
                         .Select(() => miscAlias.MarketValueAmount)
                         ).Where(a => a.Id == 123456).List<object[]>();
}

在这里,我们期待的数据现在是存储在变量结果中的对象列表,用于完全不相关的实体。谢谢!!!