6

複雑な QueryOver ステートメントよりも sprocs を実行する方が簡単で単純なレポートのインスタンスがいくつかあります。

エンティティではなく、クエリから返されたデータを表す DTO があり、クエリの結果を DTO に入力したいと考えています。名前付きクエリと session.GetNamedQuery() を使用してクエリを実行しています。

  1. DTO のマッピング ファイルを作成する必要はありますか?
  2. もしそうなら、NHibernate/FluentNHibernate に DTO 用のテーブルを作成してはならないことを知らせることは可能ですか? 私のユニット テストは、NH の SchemaExport ツールを使用してスキーマを削除および作成し、DTO のテーブルを作成したくありません。

Projections と AliasToBean を使用して QueryOver/Linq クエリを投影したくないことに注意してください。ストアド プロシージャを実行する必要があります。

乾杯

4

2 に答える 2

11

CreateSQLQuery を使用すると、マッピング ファイルがなくても次のように動作します。たぶん、名前付きクエリで試してみることができます:

public class YourDto
{
    public int YourDtoId { get; set; }
    public string YourDtoTitle { get; set; }
}

それから

var result = yourNhSession
    .CreateSQLQuery("select yourColumn1 as YourDtoId, yourColumn2 as YourDtoTitle from YOUR_TABLE")
    .SetResultTransformer(Transformers.AliasToBean<YourDto>())
    .List<YourDto>();
于 2012-05-09T09:28:50.770 に答える
2

If you want the simplest solution I suggest you add in your architecture a micro / orm like Dapper to do this. It is just dropping a single file in your project and follow the sample. In order to me it is the best solution to pair to NH when you have to do the thinghs that are for some reason out of the entity logic.

于 2012-05-09T07:46:05.563 に答える