Applying a filter in a Fluent NHibernate mapping using a lambda referencing an inherited property?

北慕城南 提交于 2021-02-08 03:10:38

问题


Is there any way to specify in a fluent nhibernate mapping file a way to only conditionally pull values into a business entity?

My current mapping snippet is:

            HasMany(m => m.GroupUsers)
            .Table("GroupUsers")
            .KeyColumns.Add("UserEntityId")
            .Inverse()
            .Cascade.All();

Ideally, I'd like to have this (which compiles but throws a runtime error that gu isn't defined):

            HasMany(m => m.GroupUsers)
            .Table("GroupUsers")
            .KeyColumns.Add("UserEntityId")
            .Where(gu => gu.DeleteDate == null)
            .Inverse()
            .Cascade.All();

The crux of the issue is that I'd like the mapping to only pull back those group users entries with a null delete date.

Edit: Delete date is on a base class


回答1:


From the FluentNhibernate API documentation:

T Where(Expression> where)

Sets the where clause for this one-to-many relationship. Note: This only supports simple cases, use the string overload for more complex clauses.

It seems filtering for base class properties is belongs to the "complex" cases.

So you should use the Where(String) overload (I haven't tested the syntax...):

HasMany(m => m.GroupUsers)
            .Table("GroupUsers")
            .KeyColumns.Add("UserEntityId")
            .Where("DeleteDate is null")
            .Inverse()
            .Cascade.All();



回答2:


I had the same issue and since I didn't believe this should be a "complex" case I ended up changing one line in the FluentNH source code which resolved it. I made a pull request but until it gets merged (or if it doesn't get merged at all) you can build it from here: https://github.com/alexDevBR/fluent-nhibernate/

EDIT: It was merged.



来源:https://stackoverflow.com/questions/9689953/applying-a-filter-in-a-fluent-nhibernate-mapping-using-a-lambda-referencing-an-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!