簡體   English   中英

從Entity Framework查詢轉換為NHibernate的問題

[英]Trouble transitioning from Entity Framework query to NHibernate

在與Entity Framework一起專門工作了幾年之后,我試圖自學NHibernate。 我知道如何編寫查詢,也知道如何編寫帶有EF和lambda表達式的代碼,但是將其翻譯為NHibernate會讓我很困擾。

該查詢將被寫為:

SELECT fb.*
FROM foo f
INNER JOIN bar b ON f.fooid = b.fooid
INNER JOIN foobar fb ON b.barid = fb.barid
WHERE f.otherid = 1

基本上,我知道啟動表的一個鍵( foo ),並且我想返回第二個聯接表( foobar )中的所有匹配行。 在EF中,我會寫:

public IEnumerable<foobar> GetFooBarInfo(int intFooID)
{
    return db.foo.Include(f => f.bar)
                 .Include(fb => fb.bar.foobar)
                 .Where(f => f.otherentity.fooid == intFooID)
                 .Select(fb => fb.bar.foobar)
                 .ToList();
}

現在,我需要將其轉換為NHibernate。 我嘗試了幾件事,但都沒有保存,但最新版本返回1行而不是7行。 好像它在foobarbarid = 1一行,而不是在barid = 1的所有barid值中fooid = 1

public IEnumerable<foobar> GetFooBarInfo(int intFooID)
{
    foo f = null;
    bar b = null;
    foobar fb = null;

    return db.QueryOver<foo>(() => f)
             .Where(fi => fi.otherentity.fooid == intFooID)
             .Inner.JoinQueryOver(ba => ba.bar, () => b)
             .Inner.JoinQueryOver(fbar => fbar.foobar, () => fb)
             .Select(Projections.ProjectionList()
                                .Add(Projections.Property(() => fb.barid))
                                .Add(Projections.Property(() => fb.barname))
                                .Add(Projections.Property(() => fb.bardescription))
                    )
             .TransformUsing(Transformers.AliasToBean<foobar>())
             .List<foobar>();
}

我還意識到它並沒有真正填充投影/變換。 barid為0, barnamebardescription為空-這些顯然在數據庫中具有值。

我試圖在fooid上加入foobar ,但是bar有自己的主鍵foobarid 打開NHibernate的日志記錄后,我可以看到生成的查詢不正確 它正在生成:

SELECT fb.*
FROM foo f
INNER JOIN bar b ON f.fooid = b.foobarid
INNER JOIN foobar fb ON b.barid = fb.barid
WHERE f.otherid = 1

因此,我意識到我需要包括bar的父表,該表是baz以便正確連接。 但這意味着我需要為baz制作地圖,包括:

.HasMany(x => x.bar).KeyColumn("fooid")
.HasMany(x => x.foobar).KeyColumn("barid")

然后更改QueryOver以包括此附加表。 結果計數正確,但是仍然無法正確投影到對象中-一切都為空,直到我在每個Projection.Property()上添加了字符串別名:

public IEnumerable<foobar> GetFooBarInfo(int intFooID)
{
    foo f = null;
    bar b = null;
    baz bz = null;
    foobar fb = null;

    return db.QueryOver<foo>(() => f)
             .Where(fi => fi.otherentity.fooid == intFooID)
             .Inner.JoinQueryOver(bb => bb.baz, () => bz)
             .Inner.JoinQueryOver(ba => ba.bar, () => b)
             .Inner.JoinQueryOver(fbar => fbar.foobar, () => fb)
             .Select(Projections.ProjectionList()
                      .Add(Projections.Property(() => fb.barid), "barid")
                      .Add(Projections.Property(() => fb.barname), "barname")
                      .Add(Projections.Property(() => fb.bardescription), "bardescription")
             )
             .TransformUsing(Transformers.AliasToBean<foobar>())
             .List<foobar>();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM