问题
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