【问题标题】:Fluent Nhibernate inner join流畅的 Nhibernate 内部连接
【发布时间】:2011-12-16 12:43:09
【问题描述】:

我有 3 个表(Master、Imagen、Linea)是:

Master
public virtual int Id { get; private set; }
public virtual Imagen imagen { get; set; }

Imagen
public virtual int Id { get; private set; }
public virtual Linea linea { get; set; }

Linea
public virtual int Id { get; private set; }
public virtual String Nombre { get; set; }

我需要这样的查询:

SELECT * FROM dbo.Master 
INNER JOIN dbo.Imagen ON dbo.Master.imagen_id = dbo.Imagen.Id 
INNER JOIN dbo.Linea ON dbo.Imagen.linea_id = dbo.Linea.Id 
WHERE dbo.Linea_Id = 5

但我不知道如何告诉 Fluent Nhibernate 使用自动映射器创建此查询。 到目前为止,我已经尝试过:

ICriteria c = session.CreateCriteria(typeof(Master))
  .CreateAlias("dbo.Imagen", "img", JoinType.InnerJoin)
  .Add(Restrictions.Eq("img.linea_id", id_linea));

return c.List<Master>();

但我收到此错误: 无法解析属性:dbo of: ImageManager.Model.Entity.Master

关于如何进行内部联接的任何想法? 提前致谢

【问题讨论】:

    标签: c# nhibernate mapping inner-join fluent


    【解决方案1】:

    首先,我会从 dbo.Imagen 中删除 dbo。使用 ICriteria 接口时,您需要考虑对象,而不是数据库表,即使对象到表和属性到列的映射可能是一对一的。

    编辑:
    另一种选择是使用 QueryOver Lambda 语法。

       var list = session.QueryOver<Master>()
                            .JoinQueryOver(master => master.imagen)
                            .Where(imagen => imagen.linea.Id == 5)
                            .List();
    

    【讨论】:

    • 谢谢,Alredy 做到了。我对 Fluent Nhibernate 真的很陌生。
    • 我试过了,但我得到了这个错误:“无法解析属性:imagen.linea.Id of: ImageManager.Model.Entity.Master”我也改为.Where(Master => Master。 imagen.linea.Id == 5) 但我得到同样的错误
    • 抱歉……我自己还在学习 QueryOver 语法。会修复的。
    • 它有效!!!,非常感谢。无论如何,我最终使用了 ICriteria,因为我需要获取与该主设备相关的图像和线条,这就是为什么我为 imagen 和 hdd 添加了 createalias,如果我没有这样做,当我尝试访问图像或 hdd 属性时,我会得到一个 LazyLoadException我的主对象。
    • 这里是代码:ICriteria c = session.CreateCriteria(typeof(MasterHdd)) .CreateAlias("imagen", "img", JoinType.InnerJoin) .CreateAlias("hdd", "h" , JoinType.InnerJoin) .CreateAlias("img.linea", "lin", JoinType.InnerJoin) .Add(Restrictions.Eq("lin.Id", id_linea)); return c.List();
    【解决方案2】:

    如果您已经修复了它,我无法从您以前的 cmets 中解决问题,但我会尝试

    ICriteria c = session.CreateCriteria(typeof(Master))
    
    .CreateAlias("imagen", "img", JoinType.InnerJoin)
    .CreateAlias("img.linea", "lin", JoinType.InnerJoin)
    .Add(Restrictions.Eq("lin.Id", 5));
    
    return c.List<Master>();
    

    编辑:外壳更改如下所述。

    【讨论】:

    • 非常感谢,我目前正在处理这个问题。问题是它不起作用。这是我得到的错误:“无法解析属性:Imagen of:ImageManager.Model.Entity.Master”我使用自动映射来映射模型文件夹下的所有内容:.Mappings(m => m.AutoMappings.Add(model)))在模型文件夹中,我有一个名为 Entity 的子文件夹,并且有实体:
    • public class Master{ public virtual int Id { get;私人套装; } 公共虚拟 Imagen imagen { 获取;放; } 公共虚拟硬盘硬盘 { 获取;放; } 公共虚拟字符串 hash_master { 获取;放; } }
    • public class Linea{ public virtual int Id { get;私人套装; } 公共虚拟 int ObjVersion { 获取;放; } 公共虚拟字符串 Nombre { 获取;放; } 公共虚拟字符串表 { 获取;放; } 公共虚拟字符串 NombreCorto { 获取;放; } } 公共类 Imagen{ 公共虚拟 int Id { 获取;私人套装; } 公共虚拟字符串 PartNumber { 获取;放; } 公共虚拟字符串版本 { 获取;放; } 公共虚拟字符串所以 { 得到;放; } 公共虚拟字符串修订 { 获取;放; } 公共虚拟 Linea linea { 获取;放; } 公共虚拟字符串 Nombre { 获取;放; } }
    • 我认为这个答案的唯一问题是对象引用的情况。你有 Imagen,其中@lloiacon 的对象引用都是小写的 - imagen。 Linea 参考将是相同的。这可能是它不起作用的原因。
    • 好地方。我的答案中的外壳发生了变化。
    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2014-09-17
    • 2011-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-27
    相关资源
    最近更新 更多