Skip to main content
 首页 » 编程设计

c#之如何在流利的 nhibernate 中为引用添加条件

2024年02月20日10zlslch

在我的 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  

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

请您参考如下方法:

在 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子句中需要使用别名,否则会出错。