【问题标题】:How to add a condition for a reference in fluent nhibernate?如何在流畅的nhibernate中添加参考条件?
【发布时间】:2012-09-05 08:37:40
【问题描述】:

在我的 ClassMap 中,我只想在满足条件时加载一个属性。这是我现在使用的代码:

References<MyObject>(x => x.Property).ForeignKey("RecordId");

我想为此添加一个 Where 子句:仅在数据库中的值为零时加载 x.Property,如下所示:

References<User>(x => x.Property).ForeignKey("RecordId").Where("Removed = 0"); // Where Removed is a column of the user table 

但不幸的是,这不起作用。有人知道这方面的等价物吗?

【问题讨论】:

    标签: c# nhibernate fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    ClassMap 类中,我添加了Where 子句:

    public class ClassAMap : ClassMap<ClassA>
    {
        public ClassAMap()
        {
            Table("ClassA");
    
            // Only load Class A where the user is not removed
            Where("PersonRecId in (select u.Userid from Users u where u.Removed = 0)");
    
            Id(c => c.Id).GeneratedBy.Identity();
    
            References<User>(x => x.User).ForeignKey("UserId");
        }
    }
    

    注意:Where子句中需要使用别名,否则会出错。

    【讨论】:

      猜你喜欢
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多